Systemd in Linux

Systemd in Linux


Systemd, init – systemd system and service manager for Linux operating systems. When run as first process on boot (as PID 1), it acts as init system that brings up and maintains userspace services. This program provides a method for activating system resources, server daemons, and other process both at boot time and on a running system.

Daemons are process that wait or run in background performing various task. Generally daemon starts automatically at boot time and continue to run until shutdown or manually stopped. Daemons uses socket to communicate with local or remote client.

Systemd is replacement for System V Init program. Systemd provide legacy support.

Init scripts are found in /etc/init.d

Systemd scripts are found in /etc/sysconfig


systemctl – Control the systemd system and service manager. Systemctl command is used to manage different systemd objects, called Units.

List available Unit types:
[root@localhost ~]# systemctl -t help
Available unit types:

The system’s copy of unit files are generally kept in the /lib/systemd/system directory. When software installs unit files on the system, this is the location where they are placed by default.

If you wish to modify the way that a unit functions, the best location to do so is within the /etc/systemd/system directory. Unit files found in this directory location take precedence over any of the other locations on the filesystem. If you need to modify the system’s copy of a unit file, putting a replacement in this directory is the safest and most flexible way to do this.

More on Unit files Click Here

Some systemctl commands:

Syntax: systemctl status <name.type>

[root@localhost ~]# systemctl status sshd.service
sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2018-06-25 09:42:15 IST; 1h 46min ago
Docs: man:sshd(8)
Main PID: 1139 (sshd)
Tasks: 1
CGroup: /system.slice/sshd.service
└─1139 /usr/sbin/sshd -D
Jun 25 09:42:14 localhost.localdomain systemd[1]: Starting OpenSSH server daemon...
Jun 25 09:42:15 localhost.localdomain sshd[1139]: Server listening on port 22.
Jun 25 09:42:15 localhost.localdomain sshd[1139]: Server listening on :: port 22.
Jun 25 09:42:15 localhost.localdomain systemd[1]: Started OpenSSH server daemon.

Query the state of all unit files:
[root@localhost ~]# systemctl

Query the state of any specific type unit:
Syntax: systemctl ––type=<type>

Systemctl --type=service

Adding ––all option will list inactive one’s also

is-active and is-enabled

[root@localhost ~]# systemctl is-active sshd.service
[root@localhost ~]# systemctl is-enabled sshd.service

View the enabled or disabled setting for unit files

Syntax: systemctl list-unit-files ––type=<type>

systemctl list-unit-files --type=service

View failed.

Syntax : systemctl ––failed ––type=<type>

systemctl --failed --type=service

Note: Execute all command as root.

To enable a service(start it automatically at boot)

systemctl enable <name.type>

To disable

systemctl disable <name.type>

Using systemd for Power Management


To shutdown the system, you can use:
systemctl poweroff

To reboot the system, you can use:
systemctl reboot

To suspend the system, you can use:
systemctl suspend

Check man systemctl for more options on power management.

Create a simple daemon:


echo $(date) > /home/hackaholic/test.txt

Give the execute permission to the script

chmod +x

Put the below content in file /etc/systemd/system/test.service

Description= A test daemon

Reload daemon

systemctl daemon-reload

Start test service

systemctl start test.service

Enable test.service, so that it starts automatically upon boot:

systemctl enable test.service
Created symlink from /etc/systemd/system/ to /etc/systemd/system/test.service.

[root@localhost ~]# cat test.txt
Tue Jun 26 09:57:00 IST 2018

Analyze system boot-up performance:

systemd-analyze – Analyze system boot-up performance
systemd-analyze may be used to determine system boot-up performance statistics and retrieve other state and tracing information from the system and service manager, and to verify the correctness of unit files.

Some useful systemd-analyze command

systemd-analyze time
Prints the time spent in the kernel before userspace has been reached, the time spent in the initial RAM disk (initrd) before normal system userspace has been reached, and the time normal system userspace took to initialize. Note that these measurements simply measure the time passed up to the point where all system services have been spawned, but not necessarily until they fully finished initialization or the disk is idle.
[root@localhost ~]# systemd-analyze time
Startup finished in 586ms (kernel) + 2.714s (initrd) + 27.219s (userspace) = 30.520s

systemd-analyze blame
Prints a list of all running units, ordered by the time they took to initialize. This information may be used to optimize boot-up times. Note that the output might be misleading as the initialization of one service might be slow simply because it waits for the initialization of another service to complete.
[root@localhost ~]# systemd-analyze blame

systemd-analyze plot:
Prints an SVG graphic detailing which system services have been started at what time, highlighting the time they spent on initialization.
[root@localhost ~]# systemd-analyze plot > system_boot.svg
The image is too big to upload i am just upload a screenshot of it.

Bookmark the permalink.

One Comment

  1. By Kumar Shubham, thank you ever so for you post.Much thanks again.

Leave a Reply

Your email address will not be published. Required fields are marked *