On Unix-like operating systems, telinit and init provide process control initialization. They are used by the system when it boots.
This page covers the Linux versions of telinit and init.
Description
init is the parent of all processes. Its primary role is to create processes from a script stored in the file /etc/inittab. This file usually has entries which cause init to spawn gettys on each line that users can log in. It also controls autonomous processes required by any particular system.
- Description
- Syntax
- Runlevels
- Booting
- Changing runlevels
- Environment
- Telinit
- Interface
- Signals
- Files
- Init bootflags
- Related commands
- Linux commands help
Syntax
/sbin/init [ -a ] [ -s ] [ -b ] [ -z xxx ] [ 0123456Ss ]
/sbin/telinit [ -t sec ] [ 0123456sSQqabcUu ]
Runlevels
A runlevel is a software configuration of the system that allows only a selected group of processes to exist. 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 down into single user mode. Runlevel S is not really meant to be used directly, but more for the scripts that are executed when entering runlevel 1.
Runlevels 7 - 9 are also valid, though not really documented. This is because “traditional” Unix variants don’t use them. In case you’re curious, runlevels S and s are in fact 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 bring the system to single user mode and do not require an /etc/inittab file. In single user mode, a root shell is opened on /dev/console.
When entering single user mode, init initializes the consoles stty settings to sane values. “CLocal” mode is set. Hardware speed and handshaking are not changed.
When entering a multi-user mode for the first time, init performs the boot and bootwait entries to allow file systems to be mounted 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 that these files exist.
Changing runlevels
After it has spawned all of the processes specified, init waits for one of its descendant processes to die, for a “powerfail” signal, or until it is signaled by telinit to change the system’s runlevel. When one of the above 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 (FAIL) - Power is failing, UPS is providing the power. Execute the powerwait and powerfail entries.
- O (OK) - The power was restored, execute the powerokwait entries.
- L (LOW) - The power is failing and the UPS has a low battery. Execute the powerfailnow entries.
If /etc/powerstatus doesn’t exist or contains anything else then the letters F, O or L, init will behave as if it has read the letter F.
Usage of SIGPWR and /etc/powerstatus is discouraged. Someone wanting to interact with init should use the /dev/initctl control channel. More information about this is available by viewing the source code of the sysvinit package.
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 5 seconds before forcibly terminating these processes via the SIGKILL signal. Note that init assumes that all these processes (and their descendants) remain in the same process group which init created for them. If any process changes its process group affiliation, it will not receive these signals. Such processes need to be terminated separately.
Environment
Init sets the following environment variables for all its children:
Telinit
/sbin/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:
telinit can also tell init how long it should wait between sending processes the SIGTERM and SIGKILL signals. The default is 5 seconds, but this can be changed with the -t sec option.
telinit can be invoked only by users with appropriate privileges.
The init binary checks if it’s 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 one can also use init instead as a shortcut.
Interface
Init listens on a fifo in /dev (/dev/initctl) for messages. Telinit uses this to communicate with init. The interface is not very well documented; to learn more about the interface, users must view the source itself.
Signals
Init reacts to several signals, including:
Files
/etc/inittab /etc/initscript /dev/console /var/run/utmp /var/log/wtmp /dev/initctl
Init bootflags
It is possible to pass several flags to init from the boot monitor (such as LILO). Init accepts the following flags:
Related commands
kill — Send a signal to a process, affecting its behavior or killing it.login — Begin a session on a system.service — Run a System V init script.sh — The Bourne shell command interpreter.