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 a further system and user services and processes. To manage system services we need a tool which is generally systemd. With these tools, we can set the status of a service, see it. Also, we can change the mount and system snapshot issues. But we will only look for the service management side. By the way, a service is a daemon that 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.

$ systemctl
List Existing Services
List Existing Services

Print/Display Service Status with systemctl Command

To see the status of service use systemctl status. You can see process id traffic info, service name, and latest logs.

$ systemctl status httpd
Print/Display Service Status
Print/Display Service Status
  • Loaded is the service configuration path.
  • Active specify the status of the service which is Active in this example also the date when the service is started is provided.
  • Docs specifies the documentation name of the service.
  • Main PID is the service main process ID which is 59634.
  • Status is the human-friendly form of the service status where “Running, listening on: port 80” in this example.
  • Tasks is the total task count which is 213 in this example.
  • Memory is the ram or memory usage of this service which is 17.9 MB in this example.
  • CGroup is the security group of the service.
LEARN MORE  Linux SSH Server (sshd) Configuration and Security Options With Examples

Start Service with systemctl Command

We can start a service with the start option and providing the service name we want to start. In this example, we will start the service named httpd.

$ systemctl start httpd

Stop Service with systemctl Command

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 with systemctl Command

To enable a service starts in startup use systemctl enable httpd. It creates a soft link for the current init level which is multi-user.

$ systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/'

Disable Service Startup At Boot with systemctl Command

If a service is enabled it will during boot automatically. In order to prevent starting a service during boot, we have to disable it with the disable option and providing the service name. In this example, we will disable the service named httpd.

$ systemctl disable httpd
rm '/etc/systemd/system/'

Print Service AutoStart or Enabled Status

Check a service about enabled status.

$ systemctl is-enabled httpd

In Linux, UNIX systems there are 2 run levels that are mostly used. A run level is the status of a system for example if you do not need a graphic interface but need networking a multi-user you use runlevel3 or multiuser run level, if you need GUI then you change to run level 5 or graphical. In these run levels, appropriate services started. First, look current run level.

$ runlevel
N 3

Change to multi-user run level which is current

$ systemctl isolate
$ runlevel
N 3

Change to graphical run level.

$ systemctl isolate
$ runlevel
3 5

Set Default System Run Level with systemctl Command

To change default start run level or target

$ systemctl set-default
rm '/etc/systemd/system/'
ln -s '/usr/lib/systemd/system/' '/etc/systemd/system/'

$ systemctl set-default
rm '/etc/systemd/system/'
ln -s '/usr/lib/systemd/system/' '/etc/systemd/system/'

Create Service Snapshot with systemctl Command

To create a snapshot of the current system and services status use systemctl snapshot. This is not a disk snapshot just service snapshot. Say you start 25 services and do not want to do all the time you can snapshot this and then easily recall the snapshot. If you want you can give the name of the snapshot after snapshot command

$ systemctl snapshot

Revert Service To Snapshot with systemctl Command

We can reload or revert to the snapshot with the isolate command like below.

$ systemctl isolate snapshot-1.snapshot

Delete Service Snapshot with systemctl Command

Existing snapshots can be deleted with the delete option. In this example, we will delete snapshot snapshot-1.snapshot.

$ systemctl delete snapshot-1.snapshot

Show Service Parameters and Details with systemctl Command

To see service parameters and details which resides in the service file.

$ systemctl show httpd
Show Service Parameters and Details
Show Service Parameters and Details

2 thoughts on “Linux Systemctl Service Management Tutorial”

Leave a Comment