/[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.23
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/1.0 -->
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.23 2005/05/20 08:32:05 neysx Exp $ -->
5 8
6<sections> 9<sections>
10
11<version>1.20</version>
12<date>2005-05-20</date>
13
7<section> 14<section>
8<title>Runlevels</title> 15<title>Runlevels</title>
9<subsection> 16<subsection>
10<title>Booting your System</title> 17<title>Booting your System</title>
11<body> 18<body>
31services you need in order to have a successfully booted system. 38services you need in order to have a successfully booted system.
32</p> 39</p>
33 40
34<p> 41<p>
35Finally, when all scripts are executed, <c>init</c> activates the terminals 42Finally, 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>, 43(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. 44<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 45This process will then make sure you are able to log on through these terminals
39by running <c>login</c>. 46by running <c>login</c>.
40</p> 47</p>
41 48
80configuration file that specifies what actions need to be taken. This 87configuration file that specifies what actions need to be taken. This
81configuration file is <path>/etc/inittab</path>. 88configuration file is <path>/etc/inittab</path>.
82</p> 89</p>
83 90
84<p> 91<p>
85If you remember the boot sequence we have just explained to you, you will 92If 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 93that <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>: 94the following line from <path>/etc/inittab</path>:
88</p> 95</p>
89 96
90<pre caption="The system initialisation line in /etc/inittab"> 97<pre caption="The system initialisation line in /etc/inittab">
91si::sysinit:/sbin/rc sysinit 98si::sysinit:/sbin/rc sysinit
92</pre> 99</pre>
268<pre caption="Requesting a list of all services that require Postfix"> 275<pre caption="Requesting a list of all services that require Postfix">
269# <i>/etc/init.d/postfix needsme</i> 276# <i>/etc/init.d/postfix needsme</i>
270</pre> 277</pre>
271 278
272<p> 279<p>
273Finally, you can ask what dependencies the service requires but that are 280Finally, you can ask what dependencies the service requires that are missing:
274missing:
275</p> 281</p>
276 282
277<pre caption="Requesting a list of missing dependencies for Postfix"> 283<pre caption="Requesting a list of missing dependencies for Postfix">
278# <i>/etc/init.d/postfix broken</i> 284# <i>/etc/init.d/postfix broken</i>
279</pre> 285</pre>
287<title>What is rc-update?</title> 293<title>What is rc-update?</title>
288<body> 294<body>
289 295
290<p> 296<p>
291Gentoo's init system uses a dependency-tree to decide what service needs to be 297Gentoo'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 298started 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 299have to do manually, we have created tools that ease the administration of the
294and init scripts. 300runlevels and init scripts.
295</p> 301</p>
296 302
297<p> 303<p>
298With <c>rc-update</c> you can add and remove init scripts to a runlevel. The 304With <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 305<c>rc-update</c> tool will then automatically ask the <c>depscan.sh</c> script
339<subsection> 345<subsection>
340<title>Why the Need for Extra Configuration?</title> 346<title>Why the Need for Extra Configuration?</title>
341<body> 347<body>
342 348
343<p> 349<p>
344Init scripts can be quite complex. It is therefore not really interesting to 350Init 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 351have 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 352error-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. 353instance, you might want to give more options to the service itself.
348</p> 354</p>
349 355
350<p> 356<p>
351A second reason to have this configuration outside the init script is to be able 357A 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 358able to update the init scripts without the fear that your configuration
353are undone. 359changes will be undone.
354</p> 360</p>
355 361
356</body> 362</body>
357</subsection> 363</subsection>
358<subsection> 364<subsection>
385<subsection> 391<subsection>
386<title>Do I Have To?</title> 392<title>Do I Have To?</title>
387<body> 393<body>
388 394
389<p> 395<p>
390No. Writing an init script is usually not necessary as Gentoo provides 396No, writing an init script is usually not necessary as Gentoo provides
391ready-to-use init scripts for all provided services. However, you might have 397ready-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 398installed a service without using Portage, in which case you will most likely
393have to create an init script. 399have to create an init script.
394</p> 400</p>
395 401
499 505
500<p> 506<p>
501In some cases you might not require a service, but want your service to be 507In 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 508started <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) 509available 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 510<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 511same runlevel are involved). You can provide this information using the
506<c>before</c> or <c>after</c> settings. 512<c>before</c> or <c>after</c> settings.
507</p> 513</p>
508 514
509<p> 515<p>
518} 524}
519</pre> 525</pre>
520 526
521<p> 527<p>
522You can also use the "*" glob to catch all services in the same runlevel, 528You can also use the "*" glob to catch all services in the same runlevel,
523although this isn't adviseable. 529although this isn't advisable.
524</p> 530</p>
525 531
526<pre caption="Running an init script as first script in the runlevel"> 532<pre caption="Running an init script as first script in the runlevel">
527depend() { 533depend() {
528 before * 534 before *
536<body> 542<body>
537 543
538<p> 544<p>
539Next to the <c>depend()</c> functionality, you also need to define the 545Next 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 546<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 547initialize 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: 548<c>eend</c> functions to inform the user about what is happening:
543</p> 549</p>
544 550
545<pre caption="Example start() function"> 551<pre caption="Example start() function">
546start() { 552start() {
562</pre> 568</pre>
563 569
564<p> 570<p>
565Other functions you can define are: <c>stop()</c> and <c>restart()</c>. You are 571Other 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 572not 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>. 573fill these functions by itself if you use <c>start-stop-daemon</c>.
574</p>
575
576<p>
577Gentoo's init script syntax is based on the Bourne Again Shell (bash) so you are
578free to use bash-compatible constructs inside your init script.
568</p> 579</p>
569 580
570</body> 581</body>
571</subsection> 582</subsection>
572<subsection> 583<subsection>
615</p> 626</p>
616 627
617</body> 628</body>
618</subsection> 629</subsection>
619</section> 630</section>
631<section>
632<title>Changing the Runlevel Behaviour</title>
633<subsection>
634<title>Who might benefit from this?</title>
635<body>
636
637<p>
638Many laptop users know the situation: at home you need to start <c>net.eth0</c>
639while you don't want to start <c>net.eth0</c> while you're on the road (as
640there is no network available). With Gentoo you can alter the runlevel behaviour
641to your own will.
642</p>
643
644<p>
645For instance you can create a second "default" runlevel which you can boot that
646has other init scripts assigned to it. You can then select at boottime what
647default runlevel you want to use.
648</p>
649
650</body>
651</subsection>
652<subsection>
653<title>Using softlevel</title>
654<body>
655
656<p>
657First of all, create the runlevel directory for your second "default" runlevel.
658As an example we create the <path>offline</path> runlevel:
659</p>
660
661<pre caption="Creating a runlevel directory">
662# <i>mkdir /etc/runlevels/offline</i>
663</pre>
664
665<p>
666Add the necessary init scripts to the newly created runlevels. For instance, if
667you want to have an exact copy of your current <c>default</c> runlevel but
668without <c>net.eth0</c>:
669</p>
670
671<pre caption="Adding the necessary init scripts">
672<comment>(Copy all services from default runlevel to offline runlevel)</comment>
673# <i>cd /etc/runlevels/default</i>
674# <i>for service in *; do rc-update add $service offline; done</i>
675<comment>(Remove unwanted service from offline runlevel)</comment>
676# <i>rc-update del net.eth0 offline</i>
677<comment>(Display active services for offline runlevel)</comment>
678# <i>rc-update show offline</i>
679<comment>(Partial sample Output)</comment>
680 acpid | offline
681 domainname | offline
682 local | offline
683 net.eth0 |
684</pre>
685
686<p>
687Now edit your bootloader configuration and add a new entry for the
688<c>offline</c> runlevel. For instance, in <path>/boot/grub/grub.conf</path>:
689</p>
690
691<pre caption="Adding an entry for the offline runlevel">
692title Gentoo Linux Offline Usage
693 root (hd0,0)
694 kernel (hd0,0)/kernel-2.4.25 root=/dev/hda3 <i>softlevel=offline</i>
695</pre>
696
697<p>
698VoilĂ , you're all set now. If you boot your system and select the newly added
699entry at boot, the <c>offline</c> runlevel will be used instead of the
700<c>default</c> one.
701</p>
702
703</body>
704</subsection>
705<subsection>
706<title>Using bootlevel</title>
707<body>
708
709<p>
710Using <c>bootlevel</c> is completely analogous to <c>softlevel</c>. The only
711difference here is that you define a second "boot" runlevel instead of a second
712"default" runlevel.
713</p>
714
715</body>
716</subsection>
717</section>
620</sections> 718</sections>

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

  ViewVC Help
Powered by ViewVC 1.1.20