Understanding the need for systemd
Hello Open source geeks, this is my first series in Linux and i will focus on development of services in linux using systemd. Lets get started
Linux init system history
Init is short for initialization, the init system starts running upon booting up of the operating system. The main function of the init system is to manage processes and services. After successful bootup, each processes being created are assigned a Process Identification Number (PID), the init system always have a PID of 1. Every process is either a child or grandchild of the init process.
Why SysV init was replaced by systemd
- Lengthy boot time: The sysV init machine takse a longer time to boot due to the fact that each of its processes and services starts in sequential order, in orther words one after the other. Imagine having tons of services to start expecially on server machines.
- sysV init codes are complex and convoluted
Here is an example of a sysV init script
#!/bin/bash
#
# chkconfig: 35 90 12
# description: Foo server
#
# Get function from functions library
. /etc/init.d/functions
# Start the service FOO
start() {
initlog -c "echo -n Starting FOO server: "
/path/to/FOO &
### Create the lock file ###
touch /var/lock/subsys/FOO
success $"FOO server startup"
echo
}
# Restart the service FOO
stop() {
initlog -c "echo -n Stopping FOO server: "
killproc FOO
### Now, delete the lock file ###
rm -f /var/lock/subsys/FOO
echo
}
### main logic ###
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status FOO
;;
restart|reload|condrestart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart|reload|status}"
exit 1
esac
exit 0
- sysv init implementation is inconsistent for every services being created.
Why is systemd a perfect init system
- systemd's script is simple to write
Here is an example of a systemd script
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
- systemd's implementation is consistent across platforms, no special commands, all commands are the same.
- In contrast to sysV init, systemd boots in parallel which makes boot time shorter and faster
- systemd has high security implementation as you can determine what resources or network permission your service or process can have.
In the next episode, i will explain the directories and service files in linux.