/[gentoo]/xml/htdocs/doc/en/handbook/hb-working-rcscripts.xml
Gentoo

Diff of /xml/htdocs/doc/en/handbook/hb-working-rcscripts.xml

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.8 Revision 1.31
1<?xml version='1.0' encoding='UTF-8'?>
2<!DOCTYPE sections SYSTEM "/dtd/book.dtd">
3
1<!-- 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 -->
2<!-- See http://creativecommons.org/licenses/by-sa/1.0 --> 5<!-- See http://creativecommons.org/licenses/by-sa/2.5 -->
3 6
4<!-- $Header: /var/cvsroot/gentoo/xml/htdocs/doc/en/handbook/hb-working-rcscripts.xml,v 1.8 2004/01/06 09:57:49 swift Exp $ --> 7<!-- $Header: /var/cvsroot/gentoo/xml/htdocs/doc/en/handbook/hb-working-rcscripts.xml,v 1.31 2007/10/11 20:09:02 nightmorph Exp $ -->
5 8
6<sections> 9<sections>
10
11<abstract>
12Gentoo uses a special initscript format which, amongst other features, allows
13dependency-driven decisions and virtual initscripts. This chapter explains all
14these aspects and explains how to deal with these scripts.
15</abstract>
16
17<version>1.25</version>
18<date>2007-10-11</date>
19
7<section> 20<section>
8<title>Runlevels</title> 21<title>Runlevels</title>
9<subsection> 22<subsection>
10<title>Booting your System</title> 23<title>Booting your System</title>
11<body> 24<body>
31services you need in order to have a successfully booted system. 44services you need in order to have a successfully booted system.
32</p> 45</p>
33 46
34<p> 47<p>
35Finally, when all scripts are executed, <c>init</c> activates the terminals 48Finally, when all scripts are executed, <c>init</c> activates the terminals
36(in most cases just the virtual consoles which are hidden beneith <c>Alt-F1</c>, 49(in most cases just the virtual consoles which are hidden beneath <c>Alt-F1</c>,
37<c>Alt-F2</c>, etc.) attaching a special process called <c>agetty</c> to it. 50<c>Alt-F2</c>, etc.) attaching a special process called <c>agetty</c> to it.
38This process will then make sure you are able to log on through these terminals 51This process will then make sure you are able to log on through these terminals
39by running <c>login</c>. 52by running <c>login</c>.
40</p> 53</p>
41 54
80configuration file that specifies what actions need to be taken. This 93configuration file that specifies what actions need to be taken. This
81configuration file is <path>/etc/inittab</path>. 94configuration file is <path>/etc/inittab</path>.
82</p> 95</p>
83 96
84<p> 97<p>
85If you remember the boot sequence we have just explained to you, you will 98If you remember the boot sequence we have just described, you will remember
86remember that <c>init</c>'s first action is to mount all filesystems. This is 99that <c>init</c>'s first action is to mount all filesystems. This is defined in
87defined in the following line from <path>/etc/inittab</path>: 100the following line from <path>/etc/inittab</path>:
88</p> 101</p>
89 102
90<pre caption="The system initialisation line in /etc/inittab"> 103<pre caption="The system initialisation line in /etc/inittab">
91si::sysinit:/sbin/rc sysinit 104si::sysinit:/sbin/rc sysinit
92</pre> 105</pre>
268<pre caption="Requesting a list of all services that require Postfix"> 281<pre caption="Requesting a list of all services that require Postfix">
269# <i>/etc/init.d/postfix needsme</i> 282# <i>/etc/init.d/postfix needsme</i>
270</pre> 283</pre>
271 284
272<p> 285<p>
273Finally, you can ask what dependencies the service requires but that are 286Finally, you can ask what dependencies the service requires that are missing:
274missing:
275</p> 287</p>
276 288
277<pre caption="Requesting a list of missing dependencies for Postfix"> 289<pre caption="Requesting a list of missing dependencies for Postfix">
278# <i>/etc/init.d/postfix broken</i> 290# <i>/etc/init.d/postfix broken</i>
279</pre> 291</pre>
287<title>What is rc-update?</title> 299<title>What is rc-update?</title>
288<body> 300<body>
289 301
290<p> 302<p>
291Gentoo's init system uses a dependency-tree to decide what service needs to be 303Gentoo's init system uses a dependency-tree to decide what service needs to be
292started first. As this is a tedious task that we wouldn't want our users to do 304started first. As this is a tedious task that we wouldn't want our users to
293manually, we have created tools that ease the administration of the runlevels 305have to do manually, we have created tools that ease the administration of the
294and init scripts. 306runlevels and init scripts.
295</p> 307</p>
296 308
297<p> 309<p>
298With <c>rc-update</c> you can add and remove init scripts to a runlevel. The 310With <c>rc-update</c> you can add and remove init scripts to a runlevel. The
299<c>rc-update</c> tool will then automatically ask the <c>depscan.sh</c> script 311<c>rc-update</c> tool will then automatically ask the <c>depscan.sh</c> script
321<pre caption="Removing Postfix from the default runlevel"> 333<pre caption="Removing Postfix from the default runlevel">
322# <i>rc-update del postfix default</i> 334# <i>rc-update del postfix default</i>
323</pre> 335</pre>
324 336
325<p> 337<p>
326The <c>rc-update show</c> command will show all the available init scripts and 338The <c>rc-update -v show</c> command will show all the available init scripts and
327list at which runlevels they will execute: 339list at which runlevels they will execute:
328</p> 340</p>
329 341
330<pre caption="Receiving init script information"> 342<pre caption="Receiving init script information">
331# <i>rc-update show</i> 343# <i>rc-update -v show</i>
344</pre>
345
346<p>
347You can also run <c>rc-update show</c> (without <c>-v</c>) to just view enabled
348init scripts and their runlevels.
332</pre> 349</p>
333 350
334</body> 351</body>
335</subsection> 352</subsection>
336</section> 353</section>
337<section> 354<section>
339<subsection> 356<subsection>
340<title>Why the Need for Extra Configuration?</title> 357<title>Why the Need for Extra Configuration?</title>
341<body> 358<body>
342 359
343<p> 360<p>
344Init scripts can be quite complex. It is therefore not really interesting to 361Init scripts can be quite complex. It is therefore not really desirable to
345have the users directly edit the init script, as it would make it more 362have the users edit the init script directly, as it would make it more
346error-prone. It is however important to be able to configure such a service. For 363error-prone. It is however important to be able to configure such a service. For
347instance, you might want to give more options to the service itself. 364instance, you might want to give more options to the service itself.
348</p> 365</p>
349 366
350<p> 367<p>
351A second reason to have this configuration outside the init script is to be able 368A second reason to have this configuration outside the init script is to be
352to update the init scripts without being afraid that your configuration changes 369able to update the init scripts without the fear that your configuration
353are undone. 370changes will be undone.
354</p> 371</p>
355 372
356</body> 373</body>
357</subsection> 374</subsection>
358<subsection> 375<subsection>
366configuration file called <path>/etc/conf.d/apache2</path>, which can contain 383configuration file called <path>/etc/conf.d/apache2</path>, which can contain
367the options you want to give to the Apache 2 server when it is started: 384the options you want to give to the Apache 2 server when it is started:
368</p> 385</p>
369 386
370<pre caption="Variable defined in /etc/conf.d/apache2"> 387<pre caption="Variable defined in /etc/conf.d/apache2">
371APACHE2_OPTS="-D PHP4" 388APACHE2_OPTS="-D PHP5"
372</pre> 389</pre>
373 390
374<p> 391<p>
375Such a configuration file contains variables and variables alone (just like 392Such a configuration file contains variables and variables alone (just like
376<path>/etc/make.conf</path>), making it very easy to configure services. It also 393<path>/etc/make.conf</path>), making it very easy to configure services. It also
385<subsection> 402<subsection>
386<title>Do I Have To?</title> 403<title>Do I Have To?</title>
387<body> 404<body>
388 405
389<p> 406<p>
390No. Writing an init script is usually not necessary as Gentoo provides 407No, writing an init script is usually not necessary as Gentoo provides
391ready-to-use init scripts for all provided services. However, you might have 408ready-to-use init scripts for all provided services. However, you might have
392installed a service without using Portage, in which case you will most likely 409installed a service without using Portage, in which case you will most likely
393have to create an init script. 410have to create an init script.
394</p> 411</p>
395 412
499 516
500<p> 517<p>
501In some cases you might not require a service, but want your service to be 518In some cases you might not require a service, but want your service to be
502started <c>before</c> (or <c>after</c>) another service <e>if</e> it is 519started <c>before</c> (or <c>after</c>) another service <e>if</e> it is
503available on the system (note the conditional - this is no dependency anymore) 520available on the system (note the conditional - this is no dependency anymore)
504<e>and</e> ran in the same runlevel (note the conditional - only services in the 521<e>and</e> run in the same runlevel (note the conditional - only services in the
505same runlevel are involved). You can provide this information using the 522same runlevel are involved). You can provide this information using the
506<c>before</c> or <c>after</c> settings. 523<c>before</c> or <c>after</c> settings.
507</p> 524</p>
508 525
509<p> 526<p>
518} 535}
519</pre> 536</pre>
520 537
521<p> 538<p>
522You can also use the "*" glob to catch all services in the same runlevel, 539You can also use the "*" glob to catch all services in the same runlevel,
523although this isn't adviseable. 540although this isn't advisable.
524</p> 541</p>
525 542
526<pre caption="Running an init script as first script in the runlevel"> 543<pre caption="Running an init script as first script in the runlevel">
527depend() { 544depend() {
528 before * 545 before *
529} 546}
530</pre> 547</pre>
531 548
549<p>
550If your service must write to local disks, it should need <c>localmount</c>. If
551it places anything in <path>/var/run</path> such as a pidfile, then it should
552start after <c>bootmisc</c>:
553</p>
554
555<pre caption="Example depend() function">
556depend() {
557 need localmount
558 after bootmisc
559}
560</pre>
561
532</body> 562</body>
533</subsection> 563</subsection>
534<subsection> 564<subsection>
535<title>Standard Functions</title> 565<title>Standard Functions</title>
536<body> 566<body>
537 567
538<p> 568<p>
539Next to the <c>depend()</c> functionality, you also need to define the 569Next to the <c>depend()</c> functionality, you also need to define the
540<c>start()</c> function. This one contains all the commands necessary to 570<c>start()</c> function. This one contains all the commands necessary to
541initialize your service. It is adviseable to use the <c>ebegin</c> and 571initialize your service. It is advisable to use the <c>ebegin</c> and
542<c>eend</c> functions to inform the user about what is happening: 572<c>eend</c> functions to inform the user about what is happening:
543</p> 573</p>
544 574
545<pre caption="Example start() function"> 575<pre caption="Example start() function">
546start() { 576start() {
547 ebegin "Starting my_service" 577 ebegin "Starting my_service"
548 start-stop-daemon --start --quiet --exec /path/to/my_service 578 start-stop-daemon --start --exec /path/to/my_service \
579 --pidfile /path/to/my_pidfile
549 eend $? 580 eend $?
550} 581}
551</pre> 582</pre>
552 583
553<p> 584<p>
585Both <c>--exec</c> and <c>--pidfile</c> should be used in start and stop
586functions. If the service does not create a pidfile, then use
587<c>--make-pidfile</c> if possible, though you should test this to be sure.
588Otherwise, don't use pidfiles. You can also add <c>--quiet</c> to the
589<c>start-stop-daemon</c> options, but this is not recommended unless the
590service is extremely verbose. Using <c>--quiet</c> may hinder debugging if the
591service fails to start.
592</p>
593
594<note>
595Make sure that <c>--exec</c> actually calls a service and not just a shell
596script that launches services and exits -- that's what the init script is
597supposed to do.
598</note>
599
600<p>
554If you need more examples of the <c>start()</c> function, please read the source 601If you need more examples of the <c>start()</c> function, please read the
555code of the available init scripts in your <path>/etc/init.d</path> directory. 602source code of the available init scripts in your <path>/etc/init.d</path>
556As for <c>start-stop-daemon</c>, there is an excellent man page available if you 603directory.
557need more information:
558</p>
559
560<pre caption="Getting the man page for start-stop-daemon">
561# <i>man start-stop-daemon</i>
562</pre> 604</p>
563 605
564<p> 606<p>
565Other functions you can define are: <c>stop()</c> and <c>restart()</c>. You are 607Other functions you can define are: <c>stop()</c> and <c>restart()</c>. You are
566not obliged to define these functions! Our init system is intelligent enough to 608not obliged to define these functions! Our init system is intelligent enough to
567fill these functions in herself if you use <c>start-stop-daemon</c>. 609fill these functions by itself if you use <c>start-stop-daemon</c>.
610</p>
611
612<p>
613Although you do not <e>have</e> to create a <c>stop()</c> function, here is an
614example:
615</p>
616
617<pre caption="Example stop() function">
618stop() {
619 ebegin "Stopping my_service"
620 start-stop-daemon --stop --exec /path/to/my_service \
621 --pidfile /path/to/my_pidfile
622 eend $?
623}
624</pre>
625
626<p>
627If your service runs some other script (for example, bash, python, or perl),
628and this script later changes names (for example, <c>foo.py</c> to <c>foo</c>),
629then you will need to add <c>--name</c> to <c>start-stop-daemon</c>. You must
630specify the name that your script will be changed to. In this example, a
631service starts <c>foo.py</c>, which changes names to <c>foo</c>:
632</p>
633
634<pre caption="A service that starts the foo script">
635start() {
636 ebegin "Starting my_script"
637 start-stop-daemon --start --exec /path/to/my_script \
638 --pidfile /path/to/my_pidfile --name foo
639 eend $?
640}
641</pre>
642
643<p>
644<c>start-stop-daemon</c> has an excellent man page available if you need more
645information:
646</p>
647
648<pre caption="Getting the man page for start-stop-daemon">
649$ <i>man start-stop-daemon</i>
650</pre>
651
652<p>
653Gentoo's init script syntax is based on the Bourne Again Shell (bash) so you are
654free to use bash-compatible constructs inside your init script.
568</p> 655</p>
569 656
570</body> 657</body>
571</subsection> 658</subsection>
572<subsection> 659<subsection>
582 669
583<pre caption="Supporting the restartdelay option"> 670<pre caption="Supporting the restartdelay option">
584opts="${opts} restartdelay" 671opts="${opts} restartdelay"
585 672
586restartdelay() { 673restartdelay() {
587 stop() 674 stop
588 sleep 3 <comment># Wait 3 seconds before starting again</comment> 675 sleep 3 <comment># Wait 3 seconds before starting again</comment>
589 start() 676 start
590} 677}
591</pre> 678</pre>
592 679
593</body> 680</body>
594</subsection> 681</subsection>
615</p> 702</p>
616 703
617</body> 704</body>
618</subsection> 705</subsection>
619</section> 706</section>
707<section>
708<title>Changing the Runlevel Behaviour</title>
709<subsection>
710<title>Who might benefit from this?</title>
711<body>
712
713<p>
714Many laptop users know the situation: at home you need to start <c>net.eth0</c>
715while you don't want to start <c>net.eth0</c> while you're on the road (as
716there is no network available). With Gentoo you can alter the runlevel behaviour
717to your own will.
718</p>
719
720<p>
721For instance you can create a second "default" runlevel which you can boot that
722has other init scripts assigned to it. You can then select at boottime what
723default runlevel you want to use.
724</p>
725
726</body>
727</subsection>
728<subsection>
729<title>Using softlevel</title>
730<body>
731
732<p>
733First of all, create the runlevel directory for your second "default" runlevel.
734As an example we create the <path>offline</path> runlevel:
735</p>
736
737<pre caption="Creating a runlevel directory">
738# <i>mkdir /etc/runlevels/offline</i>
739</pre>
740
741<p>
742Add the necessary init scripts to the newly created runlevels. For instance, if
743you want to have an exact copy of your current <c>default</c> runlevel but
744without <c>net.eth0</c>:
745</p>
746
747<pre caption="Adding the necessary init scripts">
748<comment>(Copy all services from default runlevel to offline runlevel)</comment>
749# <i>cd /etc/runlevels/default</i>
750# <i>for service in *; do rc-update add $service offline; done</i>
751<comment>(Remove unwanted service from offline runlevel)</comment>
752# <i>rc-update del net.eth0 offline</i>
753<comment>(Display active services for offline runlevel)</comment>
754# <i>rc-update show offline</i>
755<comment>(Partial sample Output)</comment>
756 acpid | offline
757 domainname | offline
758 local | offline
759 net.eth0 |
760</pre>
761
762<p>
763Even though <c>net.eth0</c> has been removed from the offline runlevel,
764<c>udev</c> will still attempt to start any devices it detects and launch the
765appropriate services. Therefore, you will need to add each network service you
766do not want started (as well as services for any other devices that may be
767started by udev) to <path>/etc/conf.d/rc</path> as shown.
768</p>
769
770<pre caption="Disabling device initiated services in /etc/conf.d/rc">
771RC_COLDPLUG="yes"
772<comment>(Next, specify the services you do not want automatically started)</comment>
773RC_PLUG_SERVICES="!net.eth0"
774</pre>
775
776<note>
777For more information on device initiated services, please see the comments
778inside <path>/etc/conf.d/rc</path>.
779</note>
780
781<p>
782Now edit your bootloader configuration and add a new entry for the
783<c>offline</c> runlevel. For instance, in <path>/boot/grub/grub.conf</path>:
784</p>
785
786<pre caption="Adding an entry for the offline runlevel">
787title Gentoo Linux Offline Usage
788 root (hd0,0)
789 kernel (hd0,0)/kernel-2.4.25 root=/dev/hda3 <i>softlevel=offline</i>
790</pre>
791
792<p>
793VoilĂ , you're all set now. If you boot your system and select the newly added
794entry at boot, the <c>offline</c> runlevel will be used instead of the
795<c>default</c> one.
796</p>
797
798</body>
799</subsection>
800<subsection>
801<title>Using bootlevel</title>
802<body>
803
804<p>
805Using <c>bootlevel</c> is completely analogous to <c>softlevel</c>. The only
806difference here is that you define a second "boot" runlevel instead of a second
807"default" runlevel.
808</p>
809
810</body>
811</subsection>
812</section>
620</sections> 813</sections>

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.31

  ViewVC Help
Powered by ViewVC 1.1.20