Linux Systemctl Service Management Tutorial
Hi, today we will learn managing Linux services with systemd or systemctl. While Linux kernel booting, it loads drivers, mount file systems, starting memory management, creating system calls etc. But after that what? Linux starts an init process to create further system and user services and processes. To manage system services we need a tool which is generally systemd. With this tools we can set a services status, see it.Also we can change mount and system snapshot issues. But we will only look for service management side. By the way a services is a daemon which works background to complete his workloads. Apache, mysql etc are services.
List Existing Services
To see all running services run systemctl command. As you can see from output systemctl list devices,ttys,services,files system related service.
Print/Display Service Status
To see status of service use systemctl status. You can see process id traffic info, service name and latest logs.
$ systemctl status httpd
To start a services use systemctl start httpd. If it starts accordingly it shows nothing as output.
$ systemctl start httpd
We can stop a service or daemon with the
stop option to the
systemctl command. In this example we will stop service named
httpd which is named
apache2 in Ubuntu.
$ systemctl stop httpd
Start Service At Boot
To enable a service start in startup use systemctl enable httpd. It creates a softlink for current initlevel which is multi-user
$ systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'
Disable Service Startup At Boot
To disable a service startup systemctl disable httpd
$ systemctl disable httpd
Print Service Startup Status At Boot
Check a service about enabled status
$ systemctl is-enabled httpd
In Linux,UNIX systems there is two run level which is mostly used. Run level is the status of a system for example if you do not need graphic interface but need networking an multi user you use runlevel3 or multiuser runlevel, if you need gui then you change to runleve 5 or graphical. In these runlevels appropriate services started.
First look current runlevel.
Change to multi user run level which is current
$ systemctl isolate multi-user.target
Change to graphical runlevel.
$ systemctl isolate graphical.target
Set Default System Run Level
To change default start run level or target
$ systemctl set-default graphical.target
ln -s '/usr/lib/systemd/system/graphical.target' '/etc/systemd/system/default.target'
$ systemctl set-default multi-user.target
ln -s '/usr/lib/systemd/system/multi-user.target' '/etc/systemd/system/default.target'
Create Service Snapshot
To create a snapshot of current system and services status use systemctl snapshot. This is not disk snapshot just service snapshot. Say you start 25 services and do not want to do all time you can snapshot this and than easily recall the snapshot. If you want you can give name of the snapshot after snaphot command
$ systemctl snapshot
Revert Service To Snapshot
We can reload or revert to the snapshot with the
isolate command like below.
$ systemctl isolate snapshot-1.snapshot
Delete Service Snapshot
Existing snapshots can be deleted with the
delete option. In this example we will delete snapshot
$ systemctl delete snapshot-1.snapshot
Show Service Parameters and Details
To see service parameters and details which resides in service file.
$ systemctl show httpd