How to Use the Init Command in Linux

Init runs at boot time and starts all the background processes

Man at computer
JGI/Tom Grill/Getty Images

Init is the parent of all Linux processes. It is the first process to start when a computer boots up, and it runs until the system shuts down. It is the ancestor of all other processes.

Its primary role is to create processes from a script stored in the file /etc/inittab. This file usually stores entries that cause Init to spawn gettys on each line that users can log in. It also controls autonomous processes required by any particular system.

Runlevels

runlevel is a software configuration of the system that allows only a selected group of processes to execute. The processes spawned by Init for each of these runlevels are defined in the /etc/inittab file. Init can be in one of eight runlevels: 0 through 6 and S or s. The runlevel is changed by having a privileged user run Telinit, which sends appropriate signals to Init, telling it which runlevel to change to.

Runlevels 0, 1, and 6 are reserved. Runlevel 0 is used to halt the system, runlevel 6 is used to reboot the system, and runlevel 1 is used to get the system into single-user mode. Runlevel S is not meant to be used directly but instead by the scripts that are executed when entering runlevel 1.

Runlevels 7 through 9 are also valid, though not well documented because "traditional" Unix variants don't use them. Runlevels S and s are the same. Internally, they are aliases for the same runlevel.

Booting

After Init is invoked as the last step of the kernel boot sequence, it looks for the file /etc/inittab to see if there is an entry of the type initdefault. The initdefault entry determines the initial runlevel of the system. If there is no such entry (or no /etc/inittab at all), a runlevel must be entered at the system console.

Runlevel S or s takes the system to single-user mode and does not require an /etc/inittab file. In single-user mode, a root shell is opened on /dev/console.

When entering single-user mode, init reads the console's ioctl states from /etc/ioctl.save. If this file does not exist, init initializes the line at 9600 baud and with CLOCAL settings. When init leaves single-user mode, it stores the console's ioctl settings in this file so it can reuse them for the next single-user session.

When entering a multiuser mode for the first time, Init performs the boot and bootwait entries to allow file systems to mount before users can log in. Then, all entries matching the runlevel are processed.

When starting a new process, Init first checks whether the file /etc/initscript exists. If it does, it uses this script to start the process.

Each time a child terminates, Init records the fact and the reason it died in /var/run/utmp and/var/log/wtmp, provided these files exist.

Changing Runlevels

After it spawns all the processes specified, Init waits for one of its descendant processes to die, a powerfail signal, or until it is signaled by Telinit to change the system's runlevel. When one of these three conditions occurs, it re-examines the /etc/inittab file. New entries can be added to this file at any time. However, init still waits for one of the above three conditions to occur. To provide for an instantaneous response, the Telinit Q or q command can wake up Init to re-examine the /etc/inittab file.

If Init is not in single-user mode and receives a powerfail signal (SIGPWR), it reads the file /etc/powerstatus. It then starts a command based on the contents of this file:

  • F(AIL): Power is failing, UPS is providing the power. Execute the powerwait and powerfail entries.
  • O(K): The power has been restored, execute the powerokwait entries.
  • L(OW): The power is failing, and the UPS has a low battery. Execute the powerfailnow entries.

If /etc/powerstatus doesn't exist or contains anything other than the letters F, O, or L, Init behaves as if it reads the letter F.

Usage of SIGPWR and /etc/powerstatus is discouraged. Anyone who wants to interact with Init should use the /dev/initctl control channel.

When Init is requested to change the runlevel, it sends the warning signal SIGTERM to all processes that are undefined in the new runlevel. It then waits five seconds before forcibly terminating these processes using the SIGKILL signal. Note that Init assumes that all these processes and their descendants remain in the same process group that Init originally created for them. If any process changes its process group affiliation, it does not receive these signals. Such processes need to be terminated separately.

Telinit

Telinit is linked to /sbin/init. It takes a one-character argument and signals Init to perform the appropriate action. The following arguments serve as directives to Telinit:

  • 0, 1, 2, 3, 4, 5, or 6: Tell Init to switch to the specified run level.
  • a, b, c: Tell Init to process only those /etc/inittab file entries having runlevel a,b, or c.
  • Q or q: Tell Init to re-examine the /etc/inittab file.
  • S or s: Tell Init to switch to single-user mode.
  • U or u: Tell init to re-execute itself (preserving the state). No re-examining of /etc/inittab file happens. Runlevel should be S, s, 1, 2, 3, 4, or 5; otherwise, the request is silently ignored.

Telinit can also tell Init how long it should wait between sending processes the SIGTERM and SIGKILL signals. The default is five seconds, but this value can be changed with the -t sec option.

Telinit can be invoked only by users with appropriate privileges.

The Init binary checks if it is Init or Telinit by looking at its process ID. The real Init's process ID is always 1. From this, it follows that instead of calling Telinit, a person can use Init as a shortcut.