SysVInit is the predecessor of systemd and is responsible for starting, stopping and managing services.
Where systemd uses configuration-files, SysVInit uses bash-scripts to manage services. These Scripts are located under /etc/init.d/*
or /etv/rc.d/init.d/*
.
Check if SysVInit is running
The initsystem has always the pid 1. So you can simply check the name of the process number 1 and see what you init system is.
[tux@server]$ ps aux | head
... /sbin/init
Unfortunately this doe not certainly mean, that you are running SysVInit.
On some distributions /sbin/init
is a symlink to systemd. So to make sure check the file /sbin/init
.
[tux@server]$ ls -l /sbin/init
If it is a normal file and not a symlink, you are using SysVInit.
Interact with Services
If you want to start or stop a service on the fly, you can do so with the service
command.
[tux@server]$ services httpd start
[tux@server]$ services httpd stop
[tux@server]$ services httpd restart
[tux@server]$ services httpd status
Alternatively you can call the script directly like so
[tux@server]$ /etc/init.d/httpd start
[tux@server]$ /etc/init.d/httpd stop
[tux@server]$ /etc/init.d/httpd restart
[tux@server]$ /etc/init.d/httpd status
Configure Service on Systemboot
Inittab
If you want a service to start up at boottime, you need to understand the concept of runlevels.
Runlevels are numbered from 0 to 6 and describe different states of a system.
Runlevel 0: System Shutdown.
Runlevel 1: Single User Mode, also known as maintenance mode.
Runlevel 2, 3, 4: Multi User Mode where Users can login via console or network.
Runlevel 5: Multi User Mode with graphical login.
Runlevel 6: System Restart.
Every Runlevel is configured to execute a specific collection of Init-scripts to archive the desired state.
The assignment between runlevel and its init-scripts is done through entries in the /etc/inittab
.
Every line has the following syntax.
id:runlevel:action:process
id
: Generic Name up to 4 characters to identify the entry.runlevel
: A List of runlevel-numbers that this entry applies to.action
: Defines how the system should start the service.process
: Name of the process
The available actions are:
boot
: Process will be executed during boot. Runlevels are ignored
bootwait
: Process will be executed during boot and the system will wait for it before continuing.sysinit
: Execute after System initialization. Runlevels are ignored.wait
: Execute for the given Runlevel and wait for the process to finish.respawn
: Restart process if it gets terminated.ctrlaltdel
: Execute if system receives ctrl + alt + del
input.
Usually respawn
is used for general services.
If you modify the /etc/inittab
SysVInit needs to reload its config. This done by
[tux@server]$ telinit q
Inittab under the hood
Every Runlevel has its own directory named /etc/rc.0
through /etc/rc.6
.
In there you will find links to the scripts in /etc/init.d/*
.
Every linkname starts with either S
or K
. This indicates if the linked services will be started or killed when switching to this Runlevel.
chkconfig
If you don’t want to mess with all these config files you can also use the chkconfig
command, if it is installed.
# enable apache webserver
[tux@server]$ chkconfig httpd on
# disable apache webserver
[tux@server]$ chkconfig httpd off
# specify runlevel 3 and 5
[tux@server]$ chkconfig --level 35 httpd on
You can also get an overview of all services and on which runlevel they will be executed.
[tux@server]$ chkconfig --list | grep httpd
httpd 0:off 1:off 2:off 3:on 4:off 5:on 6:off
Manage Runlevel
To show the current runlevel use
[tux@server]$ runlevel
N 3
In this example the runlevel is 3. The letter N
indicates that the runlevel did not change since the last boot.
To change the runlevel use
[tux@server]$ telinit 5
5
in this case is the runlevel to switch to.