]>
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 | |
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 | |
74 | implementations of a service are available, the start*/stop* jobs | |
75 | may have extra 'SERVICE' variable attached. SERVICE variable contains | |
76 | the generic service name, which can be used like this:: | |
77 | ||
78 | start on started SERVICE=syslog | |
79 | ||
80 | Job events and enabling/disabling event-base service startup | |
81 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
82 | ||
83 | Please note that relying on events not raised by PLD service jobs | |
84 | or scripts (like 'startup') will make job ignore the 'pld.no-upstart' | |
85 | setting. | |
86 | ||
87 | ||
88 | Writing Upstart job descriptions | |
89 | -------------------------------- | |
90 | ||
91 | Job description files in ``/etc/init`` are not only the recipes to start | |
92 | a service, but also configuration files for that service. Keep that in mind | |
93 | when writing the ``*.conf`` files. No complicated logic, that can change from | |
94 | a release to a release, should be implemented there, the script should be | |
95 | readable, basic configuration settings easy to find and no upstart-controlled | |
96 | settings (like resource limit) reimplemented in the script or started daemon | |
97 | arguments. | |
98 | ||
99 | The syntax of the ``/etc/init/*.conf`` files is described in the init(5) man | |
100 | page. | |
101 | ||
102 | Instead of using ``/etc/sysconfig/$service`` files put the service | |
103 | configuration directly into the ``*.conf`` file. When 'env' stanza is used for | |
104 | that, the value may be overridden when starting the job with initctl. | |
105 | ||
106 | Simple 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 |
120 | Tracking startup progress |
121 | ~~~~~~~~~~~~~~~~~~~~~~~~~ | |
122 | ||
123 | The easiest way to run a program from an upstart job is to ``exec`` it | |
124 | the way it will stay in foreground (that is what is the ``-F`` option in the | |
125 | example above for). However, when process is started this way Upstart cannot | |
126 | differentiate before the ``program starting failed`` and ``program has | |
127 | terminated`` cases. It will also assumed the job has started as soon as the | |
128 | command has been executed and that may be not what other jobs wait for. | |
129 | ||
130 | A 'proper daemon' first checks command line arguments and configuration, then | |
131 | forks two times and returns with success only when the child process is ready. | |
132 | Upstart can handle such daemons with ``expect daemon`` stanza. So, to manage | |
133 | such daemon via Upstart, exec so it daemonize and use ``expect daemon`` | |
134 | directive to tell Upstart what happens. Unfortunately, when ``expect daemon`` | |
135 | is used and the process forks only once or does some more weird thing, Upstart | |
136 | job may lock up. Also, libdaemon-based daemons don't play well with ``expect | |
137 | daemon``. | |
138 | ||
139 | When the service forks once ``expect fork`` should be used instead. | |
140 | ||
141 | There is also an ``expect stop`` option, probably the most elegant way to | |
142 | track process startup. The process doesn't have to fork in this case and | |
143 | Upstart doesn't have to track that forking. The process should raise SIGSTOP | |
144 | when it is ready – only then Upstart will emit the job's ``started`` event and | |
145 | let the process continue. Unfortunately, currently hardly anything supports | |
146 | this interface. | |
147 | ||
148 | When no ``expect`` stanza will help and we need to properly wait for process | |
149 | startup, then ``post-start`` script must be used. See the init(5) man page for | |
150 | details. | |
8b027762 JK |
151 | |
152 | Updating init scripts | |
153 | --------------------- | |
154 | ||
155 | Parts of the system will still expect ``service $name`` or even, directly, | |
156 | ``/etc/rc.d/init.d/$name`` scripts working. Also, LSB expects compatible | |
157 | init scripts in /etc/init.d. For this still to work, an upstart-controlled | |
158 | service is expected to have its ``/etc/rc.d/init.d/$name`` script also present. | |
159 | It must also be named exactly as the main upstart job for the service. | |
160 | ||
161 | The script must be a bit modified (in comparison to the traditional init | |
162 | scripts) to make use of the upstart features. | |
163 | ||
164 | For 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) | |
167 | will be ignored when upstart boot control is disabled or no upstart job is | |
168 | available for the service. Otherwise it will implement the basic LSB commands | |
169 | and exit. | |
170 | ||
171 | Sometimes some commands must be implemented in a special way (not all services | |
172 | may understand SIGHUP as a signal to reload their configuration) or extra | |
173 | commands are provided (like 'configtest'). In such case 'upstart_controlled' | |
174 | should be given a list of commands to implement. If the first argument of the | |
175 | script 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 | ||
178 | The minimal init script, for a service which will be controlled by upstart | |
179 | only 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 | ||
190 | Minimal change to an existing PLD script to handle upstart control is to add:: | |
191 | ||
192 | upstart_controlled | |
193 | ||
194 | before the usual:: | |
195 | ||
196 | RETVAL=0 | |
197 | # See how we were called. | |
198 | case "$1" in | |
199 | start) | |
200 | ||
201 | Sometimes other upstart jobs will rely on a service started by the traditional | |
202 | init script. In such case, the script should emit appropriate events. | |
203 | ||
204 | e.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 | ||
212 | The ``emit`` function does nothing when upstart-controlled boot is disabled (not | |
8c5418f1 | 213 | to trigger any upstart jobs), otherwise it calls ``/sbin/initctl emit`` |
8b027762 | 214 | |
8c5418f1 | 215 | .. |
2aa2fcb1 | 216 | vi: tw=78 ft=rst spl=en |