Linux systems have a method of managing daemon process as a service using one of init systems.
init systems have the ability to:
- Identify runlevels
- Establish dependencies
- Set the default runlevel
- Manage services
types of init systems
- SysVinit — starts and stops services based on runlevel.
- SystemD — offers not only starting and stopping services but also managing sockets, devices, mount points, swap areas and other unit types.
init daemon conf file /etc/inittab:
id:5:initdefault:
# System initialization.
si::sysinit:/etc/rc.d/rc.sysinit
l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6
runlevels
- 0 — shutdown
- 1 — single user mode — the root account is automatically logged in to the server. only command line interface is available. network services are not started.
- 2 — Multiuser mode — Users can log in, only cli is available.on some systems network interfaces and services are started and on others they are not.
- 3 — extended multiuser mode — Users can log in, only cli is available. network interfaces and services are started
- 4 — User defined — Users can customise this runlevel
- 5 — Graphical mode — Users can log in to the server. Command-line and graphical interfaces are available. Network services are started.
- 6 — reboot
While SystemD is concerned with runlevels it also manages units(group consisting of a name, type, and configuration file, focused on a particular service/action).
unit types
- automount
- device
- mount
- path
- service
- snapshot
- socket
- target
- timer
- scope
- swap
- slice
if you want to learn more about units click link below image:
SystemD services deals with service and target units
service units manages daemons (ends with .service)
target units manage group of other units (ends with .target)
systemctl list-units | grep .service
systemctl list-units | grep .target
To check services of SysVinit:
chkconfig --list
output will be something like this
0–6 runlevels
on/off — is service started or not.
List services of systemD
systemctl list-unit-files - type=service
Output:
Checking status, start and stop services with systemD vs SysVinit
SysVinit
service cups status
service cups stop
service cups start
systemD
systemctl start nginx
systemctl stop nginx
systemctl status nginx
Note: if you want certain service(nginx) to start whenever linux is started after being shut down, use command ‘systemctl enable nginx’. if you don’t want it any longer use ‘systemctl disable nginx’
Adding new services to SysVinit
- Create a new or customised service script file.
- Move the new or customised service script to /etc/rc.d/init.d
- Set appropriate permission on the script.
- Add the service to a specific runlevel.
Adding new services to SystemD
- Create a new or customised service configuration unit file for the new or customised service.
- Move the new or customised service configuration unit file to /etc/systemd/system
- Add the service to a specific target unit’s Wants to have the new or customised service start automatically with other services.
Summary
while both init and systemd are involved in system initialization and process management, systemd introduces several modern features and improvements, including a centralized logging system (Journal), more advanced service management with unit files, parallelization, and better resource management through cgroups. The choice between init and systemd often depends on the specific needs and preferences of the operating system or distribution.
my blog about systemD scripts (https://natnat1.medium.com/systemd-service-scripts-7da90e609c13)
Resources:
— book: linux bible christopher negus 10th edition