| … | |
… | |
| 2 | <!DOCTYPE sections SYSTEM "/dtd/book.dtd"> |
2 | <!DOCTYPE sections SYSTEM "/dtd/book.dtd"> |
| 3 | |
3 | |
| 4 | <!-- The content of this document is licensed under the CC-BY-SA license --> |
4 | <!-- The content of this document is licensed under the CC-BY-SA license --> |
| 5 | <!-- See http://creativecommons.org/licenses/by-sa/2.5 --> |
5 | <!-- See http://creativecommons.org/licenses/by-sa/2.5 --> |
| 6 | |
6 | |
| 7 | <!-- $Header: /var/cvsroot/gentoo/xml/htdocs/doc/en/handbook/hb-working-rcscripts.xml,v 1.25 2006/03/14 19:29:29 neysx Exp $ --> |
7 | <!-- $Header: /var/cvsroot/gentoo/xml/htdocs/doc/en/handbook/hb-working-rcscripts.xml,v 1.26 2006/09/07 08:23:02 nightmorph Exp $ --> |
| 8 | |
8 | |
| 9 | <sections> |
9 | <sections> |
| 10 | |
10 | |
| 11 | <version>1.21</version> |
11 | <version>1.22</version> |
| 12 | <date>2005-05-29</date> |
12 | <date>2006-09-07</date> |
| 13 | |
13 | |
| 14 | <section> |
14 | <section> |
| 15 | <title>Runlevels</title> |
15 | <title>Runlevels</title> |
| 16 | <subsection> |
16 | <subsection> |
| 17 | <title>Booting your System</title> |
17 | <title>Booting your System</title> |
| … | |
… | |
| 533 | depend() { |
533 | depend() { |
| 534 | before * |
534 | before * |
| 535 | } |
535 | } |
| 536 | </pre> |
536 | </pre> |
| 537 | |
537 | |
|
|
538 | <p> |
|
|
539 | If your service must write to local disks, it should need <c>localmount</c>. If |
|
|
540 | it places anything in <path>/var/run</path> such as a pidfile, then should |
|
|
541 | start after <c>bootmisc</c>: |
|
|
542 | </p> |
|
|
543 | |
|
|
544 | <pre caption="Example depend() function"> |
|
|
545 | depend() { |
|
|
546 | need localmount |
|
|
547 | after bootmisc |
|
|
548 | } |
|
|
549 | </pre> |
|
|
550 | |
| 538 | </body> |
551 | </body> |
| 539 | </subsection> |
552 | </subsection> |
| 540 | <subsection> |
553 | <subsection> |
| 541 | <title>Standard Functions</title> |
554 | <title>Standard Functions</title> |
| 542 | <body> |
555 | <body> |
| … | |
… | |
| 549 | </p> |
562 | </p> |
| 550 | |
563 | |
| 551 | <pre caption="Example start() function"> |
564 | <pre caption="Example start() function"> |
| 552 | start() { |
565 | start() { |
| 553 | ebegin "Starting my_service" |
566 | ebegin "Starting my_service" |
| 554 | start-stop-daemon --start --quiet --exec /path/to/my_service |
567 | start-stop-daemon --start --exec /path/to/my_service \ |
|
|
568 | --pidfile /path/to/my_pidfile |
| 555 | eend $? |
569 | eend $? |
| 556 | } |
570 | } |
| 557 | </pre> |
571 | </pre> |
| 558 | |
572 | |
| 559 | <p> |
573 | <p> |
|
|
574 | Both <c>--exec</c> and <c>--pidfile</c> should be used in start and stop |
|
|
575 | functions. If the service does not create a pidfile, then use |
|
|
576 | <c>--make-pidfile</c> if possible, though you should test this to be sure. |
|
|
577 | Otherwise, don't use pidfiles. You can also add <c>--quiet</c> to the |
|
|
578 | <c>start-stop-daemon</c> options, but this is not recommended unless the |
|
|
579 | service is extremely verbose. Using <c>--quiet</c> may hinder debugging if the |
|
|
580 | service fails to start. |
|
|
581 | </p> |
|
|
582 | |
|
|
583 | <note> |
|
|
584 | Make sure that <c>--exec</c> actually calls a service and not just a shell |
|
|
585 | script that launches services and exits -- that's what the init script is |
|
|
586 | supposed to do. |
|
|
587 | </note> |
|
|
588 | |
|
|
589 | <p> |
| 560 | If you need more examples of the <c>start()</c> function, please read the source |
590 | If you need more examples of the <c>start()</c> function, please read the |
| 561 | code of the available init scripts in your <path>/etc/init.d</path> directory. |
591 | source code of the available init scripts in your <path>/etc/init.d</path> |
| 562 | As for <c>start-stop-daemon</c>, there is an excellent man page available if you |
592 | directory. |
| 563 | need more information: |
|
|
| 564 | </p> |
|
|
| 565 | |
|
|
| 566 | <pre caption="Getting the man page for start-stop-daemon"> |
|
|
| 567 | # <i>man start-stop-daemon</i> |
|
|
| 568 | </pre> |
593 | </p> |
| 569 | |
594 | |
| 570 | <p> |
595 | <p> |
| 571 | Other functions you can define are: <c>stop()</c> and <c>restart()</c>. You are |
596 | Other functions you can define are: <c>stop()</c> and <c>restart()</c>. You are |
| 572 | not obliged to define these functions! Our init system is intelligent enough to |
597 | not obliged to define these functions! Our init system is intelligent enough to |
| 573 | fill these functions by itself if you use <c>start-stop-daemon</c>. |
598 | fill these functions by itself if you use <c>start-stop-daemon</c>. |
| 574 | </p> |
599 | </p> |
|
|
600 | |
|
|
601 | <p> |
|
|
602 | Although you do not <e>have</e> to create a <c>stop()</c> function, here is an |
|
|
603 | example: |
|
|
604 | </p> |
|
|
605 | |
|
|
606 | <pre caption="Example stop() function"> |
|
|
607 | stop() { |
|
|
608 | ebegin "Stopping my_service" |
|
|
609 | start-stop-daemon --stop --exec /path/to/my_service \ |
|
|
610 | --pidfile /path/to/my_pidfile |
|
|
611 | eend $? |
|
|
612 | } |
|
|
613 | </pre> |
|
|
614 | |
|
|
615 | <p> |
|
|
616 | If your service runs some other script (for example, bash, python, or perl), |
|
|
617 | and this script later changes names (for example, <c>foo.py</c> to <c>foo</c>), |
|
|
618 | then you will need to add <c>--name</c> to <c>start-stop-daemon</c>. You must |
|
|
619 | specify the name that your script will be changed to. In this example, a |
|
|
620 | service starts <c>foo.py</c>, which changes names to <c>foo</c>: |
|
|
621 | </p> |
|
|
622 | |
|
|
623 | <pre caption="A service that starts the foo script"> |
|
|
624 | start() { |
|
|
625 | ebegin "Starting my_script" |
|
|
626 | start-stop-daemon --start --exec /path/to/my_script \ |
|
|
627 | --pidfile /path/to/my_pidfile --name foo |
|
|
628 | eend $? |
|
|
629 | } |
|
|
630 | </pre> |
|
|
631 | |
|
|
632 | <p> |
|
|
633 | <c>start-stop-daemon</c> has an excellent man page available if you need more |
|
|
634 | information: |
|
|
635 | </p> |
|
|
636 | |
|
|
637 | <pre caption="Getting the man page for start-stop-daemon"> |
|
|
638 | $ <i>man start-stop-daemon</i> |
|
|
639 | </pre> |
| 575 | |
640 | |
| 576 | <p> |
641 | <p> |
| 577 | Gentoo's init script syntax is based on the Bourne Again Shell (bash) so you are |
642 | Gentoo's init script syntax is based on the Bourne Again Shell (bash) so you are |
| 578 | free to use bash-compatible constructs inside your init script. |
643 | free to use bash-compatible constructs inside your init script. |
| 579 | </p> |
644 | </p> |