What is systemd?
The systemd software suite provides the foundation for the Debian Operating system. The root process in the process tree of Linux is responsible for managing other processes and applications, and it is used to control applications in kernel-level mode. For instance, running Docker as a service.
How to Use Systemd for Various Tasks
List Unit files
Systemd employs ”unit” to handle all the system services and processes. Systemd units use configuration files to govern their different activities. Unit configuration files are classified into three types:
“default unit configuration files” with configuration files contained in the directory “/usr/lib/systemd/system”
“system-specific unit configuration files” with system-specific unit configuration files contained in “/etc/systemd/system”
“run-time unit configuration file”” with configuration files found in the directory” “/run/systemd/system”
Below is the command to list Unit files along with their output
Try using Linux grep command to filter only enabled services.
Start and Stopping Services a Service
$ sudo systemctl stop <service_name>
Below is an example of running a docker container as a service.
To run your application (Let’s call it “X”) as a systemd service, create the following file in the “/etc/systemd/system” directory and name it as
ExecStart=/usr/bin/docker start -a some_name
ExecStop=/usr/bin/docker stop -t 10 some_name
To start a service with a system boot, use the following command:
Use the following commands to stop and start the service manually:
sudo service start docker.some_name
Check Service Status
Below is the command to check the status of the running service.
Restart a Service
You can also restart a service without needing to stop and start it, which requires more effort. Below is the command to perform this task.
Reboot and Shutdown
Below are the commands to reboot and shut down the system, respectively. (As a system admin, you should also know how to shut down the system using a command line)
$ sudo systemctl poweroff
Starting Services at Boot-Time
Services like Docker need to be started as soon as your machine is turned on. To accomplish it, you can mention the below command.
Try booting your machine, and you will notice that the service starts automatically without human interaction.
In this post, we saw how systemd could perform common administration and configuration tasks with your machine. A system administrator can use the above commands to automate the configuration of services running in your machine. Make sure to try these commands with your custom services and realize the power of the systemd.