Booting a Linux installation involves multiple stages and software components,
including firmware initialization, execution of a boot loader, loading and startup of a Linux kernel image, and execution of various startup scripts and daemons. In the previous post, sysvinit is discussed. Now we look into upstart.
Upstart is an event-based replacement for the /sbin/init daemon which handles starting of tasks and services during boot, stopping them during shutdown and supervising them while the system is running.
It was originally developed for the Ubuntu distribution, but is intended to be suitable for deployment in all Linux distributions as a replacement for the venerable System-V init.
Make sure upstart version of /sbin/init is installed in the system
upstart based init File structure
One of key thing upstart is that it is backward compatible with sysvinit, i.e. all the file /etc/inittab, /etc/init.d, /etc/rc[0-6].d/ are still valid.
For upstart, there is a new directory /etc/init/, which holds Job configuration files, see below for a typical ubuntu 14.04 desktop system.
Jobs are defined in files placed in /etc/init, the name of the job is the filename under this directory without the .conf extension. They are plain text files and should not be executable.
The format treats one or more space or tabs as whitespace, which is skipped unless placed in single or double quotes. Line breaks are permitted within quotes, or if preceeded by a backslash. Comments begin with a ‘#’ and continue until the end of the line.
exec and script
All job files must have either an exec or script stanza. This specifies what will be run for the job.
script instead gives shell script code that will be executed using /bin/sh. The -e shell option is used, so any command that fails will terminate the script. The stanza is terminated by a line containing just “end script”.
Below is an example of ser2net Job, which is to start “ser2net” job when it is in runlevel 2,3,4,5. In pre-start, it makes sure it is executable.
start on and stop on
Your job is now able to be started and stopped manually by a system administrator, however you also probably want it to be started and stopped automatically when events are emitted.
The primary event emitted by upstart is startup which is when the machine is first started (without writable filesystems or networking).
If you’re using the example jobs, you will also have runlevel X events, where X is one of 0–6 or S. Jobs will be run alongside the init scripts for that runlevel.
Finally other jobs generate events as they are run; you can have yours run when another job stops by using stopped job. The other useful job event is started job.
You list the events you want to start your job with start on, and the events that stop your job with stop on.
start, stop, status
Jobs may be started and stopped manually by using the start and stop commands, usually installed into /sbin. Each takes a job name, and outputs the final status (see below).
A list of all jobs and their states can be obtained by using initctl list.
service vs job
The above we discussed is called job, which is defined by a file under /etc/init/ with extention “.conf”.
In ubuntu distribution, you will see “service”, which is essentially the task started by sysvinit way.
initctl list will show the process managed using upstart native file. service will show those using a more traditional init script. systemctl will show the one using systemd native format.