]> git.pld-linux.org Git - projects/rc-scripts.git/blame - doc/upstart.txt
- typo
[projects/rc-scripts.git] / doc / upstart.txt
CommitLineData
8b027762
JK
1===================================
2Upstart event-based service startup
3===================================
4
5This version of rc-scripts support Upstart event-based service startup. This
6can co-exist with old-style startup scripts.
7
8Enabling/disabling event-base service startup
9---------------------------------------------
10
11Upstart event-based service startup may be disabled on boot time
12by using a ``pld.no-upstart`` kernel command-line option.
13
14An init script may be called with ``USE_UPSTART=no`` environment variable
15to disable special upstart-related processing – this way one may use
16``/etc/rc.d/init.d/$service start`` to start a service even if upstart job
9fb27aac
JK
17for that service is present. The ``/sbin/service`` script has two new options
18``--upstart`` and ``--no-upstart`` to force new- or old-style service control.
8b027762
JK
19
20``USE_UPSTART=no`` can also be places in ``/etc/sysconfig/system``
9fb27aac
JK
21configuration file, though it can break ``*-upstart`` packages
22installation/removal a bit.
8b027762
JK
23
24Available events
25----------------
26
27Ubuntu-compatible system events
28~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29
30all-swaps
31 when swaps from ``/etc/fstab`` are activated
32
33filesystem
34 when basic filesystem hierarchy (FHS) is mounted
35 NOTE: currently it doesn't wait for network filesystems!
36
37local-filesystems
38 when all local filesystems are mounted and initialized
39
40root-filesystem
41 when root filesystem is mounted r/w and initialized
42
43virtual-filesystems
44 when virtual filesystems (/proc, /sys, etc.) are mounted
45
5a0e2340
ER
46PLD-specific system events:
47~~~~~~~~~~~~~~~~~~~~~~~~~~~
8b027762
JK
48
49pld.sysinit-done
50 when rc.sysinit finished its job
51
52pld.shutdown-started
53 when rc.shutdown starts
54
55pld.network-starting
56 just before network initialization is started
57
58pld.network-started
59 when network is initialized
60
61pld.network-stopping
62 just before network shutdown is started
63
64pld.network-stopped
65 when network configuration is shut down
66
67Jobs
68~~~~
69
70The standard Upstart events are available for job control:
71starting(7) started(7) stopping(7) stopped(7) (see man pages)
72
73As relying on job name is not good enough when several alternative
74implementations of a service are available, the start*/stop* jobs
75may have extra 'SERVICE' variable attached. SERVICE variable contains
76the generic service name, which can be used like this::
77
78 start on started SERVICE=syslog
79
80Job events and enabling/disabling event-base service startup
81~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
82
83Please note that relying on events not raised by PLD service jobs
84or scripts (like 'startup') will make job ignore the 'pld.no-upstart'
85setting.
86
87
88Writing Upstart job descriptions
89--------------------------------
90
91Job description files in ``/etc/init`` are not only the recipes to start
92a service, but also configuration files for that service. Keep that in mind
93when writing the ``*.conf`` files. No complicated logic, that can change from
94a release to a release, should be implemented there, the script should be
95readable, basic configuration settings easy to find and no upstart-controlled
96settings (like resource limit) reimplemented in the script or started daemon
97arguments.
98
99The syntax of the ``/etc/init/*.conf`` files is described in the init(5) man
100page.
101
102Instead of using ``/etc/sysconfig/$service`` files put the service
103configuration directly into the ``*.conf`` file. When 'env' stanza is used for
104that, the value may be overridden when starting the job with initctl.
105
106Simple example, the job description for syslog-ng::
107
108 start on pld.network-started
109 stop on pld.shutdown-started
110
111 env SERVICE=syslog
112 export SERVICE
113
114 respawn
115
116 console output
117
118 exec /usr/sbin/syslog-ng -F -f /etc/syslog-ng/syslog-ng.conf
119
2aa2fcb1
JK
120Tracking startup progress
121~~~~~~~~~~~~~~~~~~~~~~~~~
122
123The easiest way to run a program from an upstart job is to ``exec`` it
124the way it will stay in foreground (that is what is the ``-F`` option in the
125example above for). However, when process is started this way Upstart cannot
126differentiate before the ``program starting failed`` and ``program has
127terminated`` cases. It will also assumed the job has started as soon as the
128command has been executed and that may be not what other jobs wait for.
129
130A 'proper daemon' first checks command line arguments and configuration, then
131forks two times and returns with success only when the child process is ready.
132Upstart can handle such daemons with ``expect daemon`` stanza. So, to manage
133such daemon via Upstart, exec so it daemonize and use ``expect daemon``
134directive to tell Upstart what happens. Unfortunately, when ``expect daemon``
135is used and the process forks only once or does some more weird thing, Upstart
136job may lock up. Also, libdaemon-based daemons don't play well with ``expect
137daemon``.
138
139When the service forks once ``expect fork`` should be used instead.
140
141There is also an ``expect stop`` option, probably the most elegant way to
142track process startup. The process doesn't have to fork in this case and
143Upstart doesn't have to track that forking. The process should raise SIGSTOP
144when it is ready – only then Upstart will emit the job's ``started`` event and
145let the process continue. Unfortunately, currently hardly anything supports
146this interface.
147
148When no ``expect`` stanza will help and we need to properly wait for process
149startup, then ``post-start`` script must be used. See the init(5) man page for
150details.
8b027762
JK
151
152Updating init scripts
153---------------------
154
155Parts of the system will still expect ``service $name`` or even, directly,
156``/etc/rc.d/init.d/$name`` scripts working. Also, LSB expects compatible
157init scripts in /etc/init.d. For this still to work, an upstart-controlled
158service is expected to have its ``/etc/rc.d/init.d/$name`` script also present.
159It must also be named exactly as the main upstart job for the service.
160
161The script must be a bit modified (in comparison to the traditional init
162scripts) to make use of the upstart features.
163
164For the start/stop/status/reload commands to work the script should include a
165``upstart_controlled`` command placed before commands are handled (and after
166``/etc/rc.d/init.d/function`` was included). This command (shell alias actually)
167will be ignored when upstart boot control is disabled or no upstart job is
168available for the service. Otherwise it will implement the basic LSB commands
169and exit.
170
171Sometimes some commands must be implemented in a special way (not all services
172may understand SIGHUP as a signal to reload their configuration) or extra
173commands are provided (like 'configtest'). In such case 'upstart_controlled'
174should be given a list of commands to implement. If the first argument of the
175script was not one of the listed commands, processing will continue past
176'upstart_controlled' call and the commands may be handled by the init script.
177
178The minimal init script, for a service which will be controlled by upstart
179only would be::
180
181 #!/bin/sh
182
183 . /etc/rc.d/init.d/functions
184
185 upstart_controlled
186
187 echo "Service available only via upstart boot"
188 exit 3
189
190Minimal change to an existing PLD script to handle upstart control is to add::
191
192 upstart_controlled
193
194before the usual::
195
196 RETVAL=0
197 # See how we were called.
198 case "$1" in
199 start)
200
201Sometimes other upstart jobs will rely on a service started by the traditional
202init script. In such case, the script should emit appropriate events.
203
204e.g.::
205
206 msg_starting "syslog-ng"
207 emit starting JOB=_ SERVICE=syslog
208 daemon /usr/sbin/syslog-ng -f /etc/syslog-ng/syslog-ng.conf $OPTIONS
209 emit started JOB=_ SERVICE=syslog
210 RETVAL=$?
211
212The ``emit`` function does nothing when upstart-controlled boot is disabled (not
8c5418f1 213to trigger any upstart jobs), otherwise it calls ``/sbin/initctl emit``
8b027762 214
8c5418f1 215..
2aa2fcb1 216 vi: tw=78 ft=rst spl=en
This page took 0.082607 seconds and 4 git commands to generate.