07. Service management with SYSTEMD

Arindam BaidyaArindam Baidya
3 min read

Creating a SYSTEMD service

To run a command in the background, we need to define that command as a service. And for this we create a service unit file for that.

Let’s make a service unit file called project-mercury.service, and we need to create it under /etc/systemd/system/ directory.

We need to provide the path of the file (project-mercury.sh) /usr/bin/project-mercury.sh to [Service] section and value for ExecStart. Now it has become a service. It can now run in the background by running the command systemctl start project-mercury.service and stop by systemctl stop project-mercury.service. We need to use sudo till now, because the created service has been set to run as root user by default. To validate the service is running in the background or not we can check using systemctl status project-mercury.service.

To allow the service to enable during boot we need to add another section called [Install] and need to set the directive for WantedBy as the systemd target or the run level in which want to enable the service.

Now, let’s add the service account to start the service instead of root user, and to do this need to add another directive named User under the [Service].

Now set another directive for restarting the application on failure which will goes under the [Service] section and set value for Restart. And can add RestartSec directive to set the time to wait before restart.

Now with systemd, the service events are automatically logged.

If we have any service, on which our service depends. In other word, if we need to execute our service after running some another service, then we can add those under the [Unit] section by setting the value for After directive.

Before running the created service, system should be detect the changes we made/ we have created a service. To do so we need to run ** systemctl daemon-reload.

All the dependencies goes to [Unit] section; Requirement specifying the service start in which mode goes under [Install] section; Everything else goes to the [Service]** section.

SYSTEMD Tools

Tow major tools are systemctl and journalctl.

systemctl is the main command used to manage services on a SYSTEMD-managed server.

journalctl command can query the contents of SYSTEMD logging system called journal.

Here, we will take docker as a service,

systemctl start docker to start a service.

systemctl stop docker to stop a service.

systemctl restart docker to restart a service. It will attempt to bring down the service and then bring it back up.

systemctl reload docker to reload the service without interrupting normal functionality.

systemctl enable docker to enable a service and make it persistent acorss reboot.

systemctl disable docker to disable the service at boot.

systemctl status docker to know state of the service.

States

  • Active (running); Inactive (dead); Activating and Inactivating (in-between two state); Failed.

systemctl daemon-reload reloads the system manager configuration and makes the systemd aware of the changes. A running service whose unit file updated on disk, can only be restarted after running the systemctl daemon-reload command.

systemctl edit project-mercury.service --full make changes to unit files. Units edited this way apply the changes immediately without having to run the daemon- reload.

systemctl get-default to see the current run level or target.

systemctl set-default multi-user.target to set/change the run level.

systemctl list-units --all to see all the units systemd has loaded or attempted to load.

systemctl list-units to see the information about the active units.

journalctl prints all the log entries from the oldest entry to the newest.

journalctl -b to see the logs from the current boot.

journalctl -u UNIT to see the log entries for a specific unit.

  • journalctl -u docker.service

References

KodeKloud

0
Subscribe to my newsletter

Read articles from Arindam Baidya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Arindam Baidya
Arindam Baidya