Linux Init System
Currently, there are three different init systems in Linux: sysvinit, upstart, systemd.
1. Sysvinit
-
Configuration scripts are located in /etc/rc.d/init.d/.
-
Usually, each script has three functions: start, stop, and restart.
-
System has different run levels, ususally, 0-6 and S; And different scripts should run on different levels.
-
Usually, we create one fold for each run level under /etc/rc.d/, e.g. /etc/rc.d/rc0.d, /etc/rc.d/rc1.d, …, /etc/rc.d/rc6.d.
-
We create various symbol links in /etc/rc.d/rcX.d which refers to scripts in /etc/rc.d/init.d. Each link represents that this script will be run at this level when system boot up.
-
Command ‘chkconfig’ can be used to build such symbol link on RH/Fedora/CentOS. Command ‘update-rc.d’ does the same thing for Debian/Ubuntu.
-
System default run level is set in /etc/inittab.
2. Upstart
Digests from Upstart Getting Started
-
Upstart Jobs(scripts) are located in /etc/init/ , and have .conf extension. And they are plain text files and are executable.
-
All jobs files must have either an exec or script stanza. They specifies what will be run for the job.
-
Additional shell code can be given to be run before or after the job specified with exec or script. These are not expected to start the process, in fact, they are intended for preparing the environment and cleaning up afterwards.
-
Jobs can be started and stopped manually. However, they can be done automatically when events are emitted.
-
The primary event emitted by upstart is startup which is the machine is first started. For sysvinit, we can have runlevel X events, where X is one of 0-6 or S. Other jobs can also generate events as they are run. stopped job is used when another job stops. started job is used when another job starts.
-
You can change the settings of jobs output and input use console.
-
Use command start or stop to start or stop job. Use status to query job status. Use initctl list to get a list of all jobs and their states.
-
A custom event can be emitted by command initctl emit, any jobs started or stopped by this event will be affected.
3. systemd
Digests from Introduction to systemd.
-
The configuration of the services is by default in /lib/systemd/system or /usr/lib/systemd/system. It’s a good practice to store custom configurations in /etc/systemd/system because they won’t be deleted in case of system update.
-
The basic object of systemd is unit, which has a name and type, such like NetworkManager.service defines a service to handle network management, systemd-shutdown.socket sets the socket for shutdown. Systemd types include service, socket, busname, target, path, mount,…
-
Use command systemctl list-units to get all units.
-
Use command systemctl
<action
><service_name
>.service to manage service. action can be one of start, stop, restart, reload, enable, disable, mask, kill. -
Use systemctl
<action
><xxx
> to get info of a service. action can be one of list-unit-files, status, is-active, is-enabled, is-failed,… -
Use journalctl to view log information.
4. How to find out which one your machine are using?
-
check output of stat /proc/1/exe
-
if it gives /sbin/init, then try stat /sbin/init or /sbin/init –version
—END—