]>
Commit | Line | Data |
---|---|---|
8b027762 JK |
1 | =================================== |
2 | Upstart event-based service startup | |
3 | =================================== | |
4 | ||
5 | This version of rc-scripts support Upstart event-based service startup. This | |
6 | can co-exist with old-style startup scripts. | |
7 | ||
8 | Enabling/disabling event-base service startup | |
9 | --------------------------------------------- | |
10 | ||
11 | Upstart event-based service startup may be disabled on boot time | |
12 | by using a ``pld.no-upstart`` kernel command-line option. | |
13 | ||
14 | An init script may be called with ``USE_UPSTART=no`` environment variable | |
15 | to disable special upstart-related processing – this way one may use | |
3b2adafb | 16 | ``/etc/rc.d/init.d/$service start`` to start a service even if upstart job |
9fb27aac JK |
17 | for 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 |
21 | configuration file, though it can break ``*-upstart`` packages |
22 | installation/removal a bit. | |
8b027762 JK |
23 | |
24 | Available events | |
25 | ---------------- | |
26 | ||
27 | Ubuntu-compatible system events | |
28 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
29 | ||
30 | all-swaps | |
31 | when swaps from ``/etc/fstab`` are activated | |
32 | ||
33 | filesystem | |
34 | when basic filesystem hierarchy (FHS) is mounted | |
35 | NOTE: currently it doesn't wait for network filesystems! | |
36 | ||
37 | local-filesystems | |
38 | when all local filesystems are mounted and initialized | |
39 | ||
40 | root-filesystem | |
41 | when root filesystem is mounted r/w and initialized | |
42 | ||
43 | virtual-filesystems | |
44 | when virtual filesystems (/proc, /sys, etc.) are mounted | |
45 | ||
5a0e2340 ER |
46 | PLD-specific system events: |
47 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
8b027762 JK |
48 | |
49 | pld.sysinit-done | |
50 | when rc.sysinit finished its job | |
51 | ||
52 | pld.shutdown-started | |
53 | when rc.shutdown starts | |
54 | ||
55 | pld.network-starting | |
56 | just before network initialization is started | |
57 | ||
58 | pld.network-started | |
59 | when network is initialized | |
60 | ||
61 | pld.network-stopping | |
62 | just before network shutdown is started | |
63 | ||
64 | pld.network-stopped | |
65 | when network configuration is shut down | |
66 | ||
67 | Jobs | |
68 | ~~~~ | |
69 | ||
70 | The standard Upstart events are available for job control: | |
71 | starting(7) started(7) stopping(7) stopped(7) (see man pages) | |
72 | ||
73 | As relying on job name is not good enough when several alternative | |
29eedaa7 JK |
74 | implementations of a service are available. In such case |
75 | each of the alternative jobs should have an extra 'SERVICE_syslog=y' | |
76 | variable exported. In van then be used like this:: | |
8b027762 | 77 | |
29eedaa7 JK |
78 | start on started SERVICE_syslog=y |
79 | ||
80 | Please note that using 'SERVICE=something' will not work, as the value | |
81 | will be inherited by any other job with 'export SERVICE'. | |
8b027762 JK |
82 | |
83 | Job events and enabling/disabling event-base service startup | |
84 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
85 | ||
86 | Please note that relying on events not raised by PLD service jobs | |
87 | or scripts (like 'startup') will make job ignore the 'pld.no-upstart' | |
88 | setting. | |
89 | ||
90 | ||
91 | Writing Upstart job descriptions | |
92 | -------------------------------- | |
93 | ||
3b2adafb | 94 | Job description files in ``/etc/init`` are not only the recipes to start |
8b027762 | 95 | a service, but also configuration files for that service. Keep that in mind |
3b2adafb | 96 | when writing the ``*.conf`` files. No complicated logic, that can change from |
8b027762 JK |
97 | a release to a release, should be implemented there, the script should be |
98 | readable, basic configuration settings easy to find and no upstart-controlled | |
3b2adafb | 99 | settings (like resource limit) reimplemented in the script or started daemon |
8b027762 JK |
100 | arguments. |
101 | ||
102 | The syntax of the ``/etc/init/*.conf`` files is described in the init(5) man | |
103 | page. | |
104 | ||
105 | Instead of using ``/etc/sysconfig/$service`` files put the service | |
106 | configuration directly into the ``*.conf`` file. When 'env' stanza is used for | |
107 | that, the value may be overridden when starting the job with initctl. | |
108 | ||
109 | Simple example, the job description for syslog-ng:: | |
110 | ||
111 | start on pld.network-started | |
112 | stop on pld.shutdown-started | |
3b2adafb | 113 | |
8b027762 JK |
114 | env SERVICE=syslog |
115 | export SERVICE | |
3b2adafb | 116 | |
8b027762 | 117 | respawn |
3b2adafb | 118 | |
8b027762 | 119 | console output |
3b2adafb | 120 | |
8b027762 JK |
121 | exec /usr/sbin/syslog-ng -F -f /etc/syslog-ng/syslog-ng.conf |
122 | ||
29eedaa7 JK |
123 | Checking upstart configuration |
124 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
125 | ||
126 | Since Upstart 1.3 one can check current configuration with:: | |
127 | ||
128 | initctl check-config | |
129 | ||
3b2adafb | 130 | Also, with an 'initctl2dot' tool the configuration may be visualised in |
29eedaa7 JK |
131 | a graphical diagram. |
132 | ||
2aa2fcb1 JK |
133 | Tracking startup progress |
134 | ~~~~~~~~~~~~~~~~~~~~~~~~~ | |
135 | ||
136 | The easiest way to run a program from an upstart job is to ``exec`` it | |
137 | the way it will stay in foreground (that is what is the ``-F`` option in the | |
138 | example above for). However, when process is started this way Upstart cannot | |
3b2adafb ER |
139 | differentiate before the ``program starting failed`` and ``program has |
140 | terminated`` cases. It will also assumed the job has started as soon as the | |
2aa2fcb1 JK |
141 | command has been executed and that may be not what other jobs wait for. |
142 | ||
143 | A 'proper daemon' first checks command line arguments and configuration, then | |
144 | forks two times and returns with success only when the child process is ready. | |
145 | Upstart can handle such daemons with ``expect daemon`` stanza. So, to manage | |
146 | such daemon via Upstart, exec so it daemonize and use ``expect daemon`` | |
147 | directive to tell Upstart what happens. Unfortunately, when ``expect daemon`` | |
148 | is used and the process forks only once or does some more weird thing, Upstart | |
149 | job may lock up. Also, libdaemon-based daemons don't play well with ``expect | |
150 | daemon``. | |
151 | ||
152 | When the service forks once ``expect fork`` should be used instead. | |
153 | ||
154 | There is also an ``expect stop`` option, probably the most elegant way to | |
155 | track process startup. The process doesn't have to fork in this case and | |
156 | Upstart doesn't have to track that forking. The process should raise SIGSTOP | |
157 | when it is ready – only then Upstart will emit the job's ``started`` event and | |
158 | let the process continue. Unfortunately, currently hardly anything supports | |
159 | this interface. | |
160 | ||
161 | When no ``expect`` stanza will help and we need to properly wait for process | |
162 | startup, then ``post-start`` script must be used. See the init(5) man page for | |
163 | details. | |
8b027762 | 164 | |
821d0f6e JK |
165 | Debuging jobs |
166 | ~~~~~~~~~~~~~ | |
167 | ||
168 | Making sure job description is correct and Upstart will properly manage the | |
169 | process may be tricky. One way to check if the job was described and started | |
170 | properly is to use ``pstree -p`` command and compare it to ``initctl status`` | |
171 | output. Example:: | |
172 | ||
173 | # initctl status cherokee | |
174 | cherokee start/running, process 22419 | |
175 | ||
176 | # pstree -p | |
177 | init(1)─┬─Terminal(19446)─┬─bash(8983)───console(9003) | |
178 | .... | |
179 | |-bacula-sd(3514)---{bacula-sd}(3520) | |
180 | |-cherokee(22419)-+-cherokee-worker(22423)-+-rrdtool(22425) | |
181 | | | |-{cherokee-worker}(22424) | |
182 | | | |-{cherokee-worker}(22426) | |
183 | | | |-{cherokee-worker}(22427) | |
184 | | | |-{cherokee-worker}(22428) | |
185 | | | |-{cherokee-worker}(22429) | |
186 | | | |-{cherokee-worker}(22430) | |
187 | | | |-{cherokee-worker}(22431) | |
188 | | | |-{cherokee-worker}(22432) | |
189 | | | |-{cherokee-worker}(22433) | |
190 | | | `-{cherokee-worker}(22434) | |
191 | | `-{cherokee}(22422) | |
192 | |-conserver(3471)---conserver(3477) | |
193 | .... | |
194 | ||
195 | ||
196 | As you can see, Upstart thinks the main process of 'cherokee' is '22419', | |
197 | and indeed this is the only 'cherokee' child of init. So this state is correct. | |
198 | ||
199 | Common problem that may appear in pstree output: | |
200 | ||
201 | 1. The main process pid differs from what Upstart thinks. That usually | |
202 | happens when bad 'expect fork' or 'expect daemon' is used. May cause | |
203 | Upstart lock-up when the PID reported by ``initctl status`` does not exist | |
204 | at all. | |
205 | ||
206 | 2. Init has multiple children processes for one job instance. It may happen | |
207 | when previously running job was not properly killed, when bad 'expect' was | |
208 | used or when the daemon does weird forking on startup. | |
209 | ||
8b027762 JK |
210 | Updating init scripts |
211 | --------------------- | |
212 | ||
213 | Parts of the system will still expect ``service $name`` or even, directly, | |
214 | ``/etc/rc.d/init.d/$name`` scripts working. Also, LSB expects compatible | |
215 | init scripts in /etc/init.d. For this still to work, an upstart-controlled | |
3b2adafb | 216 | service is expected to have its ``/etc/rc.d/init.d/$name`` script also present. |
8b027762 JK |
217 | It must also be named exactly as the main upstart job for the service. |
218 | ||
219 | The script must be a bit modified (in comparison to the traditional init | |
220 | scripts) to make use of the upstart features. | |
221 | ||
222 | For the start/stop/status/reload commands to work the script should include a | |
223 | ``upstart_controlled`` command placed before commands are handled (and after | |
3b2adafb | 224 | ``/etc/rc.d/init.d/function`` was included). This command (shell alias actually) |
8b027762 JK |
225 | will be ignored when upstart boot control is disabled or no upstart job is |
226 | available for the service. Otherwise it will implement the basic LSB commands | |
227 | and exit. | |
228 | ||
229 | Sometimes some commands must be implemented in a special way (not all services | |
230 | may understand SIGHUP as a signal to reload their configuration) or extra | |
a2653f82 JK |
231 | commands are provided (like 'configtest'). In such case ``upstart_controlled`` |
232 | should be given a list of commands to implement or, preferrably, ``--except`` | |
233 | and the list of commands which stay implemented in the script. If the first | |
234 | argument of the script is one to be of the 'not upstart_controlled' commands, | |
235 | processing will continue past 'upstart_controlled' call and the commands may | |
236 | be handled by the init script. | |
237 | ||
238 | When ``configtest`` is includes in the ``upstart_controlled --except`` list | |
239 | then ``$script configtest`` will be called before each restart/reload attempt, | |
240 | but only when done by ``/sbin/service`` or call to the script. Direct initctl | |
241 | calls are not affected. | |
8b027762 JK |
242 | |
243 | The minimal init script, for a service which will be controlled by upstart | |
244 | only would be:: | |
245 | ||
246 | #!/bin/sh | |
3b2adafb | 247 | |
8b027762 | 248 | . /etc/rc.d/init.d/functions |
3b2adafb | 249 | |
8b027762 | 250 | upstart_controlled |
3b2adafb | 251 | |
8b027762 JK |
252 | echo "Service available only via upstart boot" |
253 | exit 3 | |
254 | ||
255 | Minimal change to an existing PLD script to handle upstart control is to add:: | |
3b2adafb | 256 | |
8b027762 JK |
257 | upstart_controlled |
258 | ||
259 | before the usual:: | |
260 | ||
261 | RETVAL=0 | |
262 | # See how we were called. | |
263 | case "$1" in | |
264 | start) | |
265 | ||
266 | Sometimes other upstart jobs will rely on a service started by the traditional | |
267 | init script. In such case, the script should emit appropriate events. | |
268 | ||
269 | e.g.:: | |
270 | ||
271 | msg_starting "syslog-ng" | |
98e096f3 | 272 | emit starting JOB=syslog-ng SERVICE=syslog |
8b027762 | 273 | daemon /usr/sbin/syslog-ng -f /etc/syslog-ng/syslog-ng.conf $OPTIONS |
98e096f3 | 274 | emit started JOB=syslog-ng SERVICE=syslog |
8b027762 JK |
275 | RETVAL=$? |
276 | ||
277 | The ``emit`` function does nothing when upstart-controlled boot is disabled (not | |
8c5418f1 | 278 | to trigger any upstart jobs), otherwise it calls ``/sbin/initctl emit`` |
8b027762 | 279 | |
8c5418f1 | 280 | .. |
2aa2fcb1 | 281 | vi: tw=78 ft=rst spl=en |