×


Run Shell Script as Systemd Service in Linux

Systemd is a service manager that provides you features of managing daemons, process tracking, mount, automount. It uses a Linux control group to manage the task. It replaces the older initd and is available in Debian, Fedora, RedHat, ubuntu, centos, arch Linux distributions.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related systemd queries on Debian Linux System.

In this context, we shall look into how to create a service using systemd.


How to Run Shell Script as Systemd Service in Linux ?

Here, we will make a manual script which will act like a process to find disk utilization of the Linux system.

To begin, Make a bash script that redirects date and disk utilization in a file. You can create files in any location. We are going to make in executable directory /usr/bin:

$ sudo vim /usr/bin/script.sh

Then, Copy and paste the following script and save your file:

#!/bin/bash
# Script generates disk utilization by the system and store in a file
while true
do
date >> /var/storage-monitor.txt
sudo du -sch / >> /var/storage-monitor.txt
sleep 120
done

Next, Make the file executable by running the following command.

$ chmod +x /usr/bin/script.sh

Now, let's make a service for running the script. Just create a file in the following directory. Note you can give any name but it must end with .service extension:

$ vim /etc/systemd/system/monitor-disk.service

And add the following details:

[Unit]
Description=My disk monitoring service
Documentation=https://www.kernel.org/
#After=networking.service
[Service]
Type=simple
User=root
Group=root
TimeoutStartSec=0
Restart=on-failure
RestartSec=30s
#ExecStartPre=
ExecStart=/usr/bin/script.sh
SyslogIdentifier=Diskutilization
#ExecStop=
[Install]
WantedBy=multi-user.target

What is going on here is:

  • The [Unit] section consists of description, documentation details. Here we have mentioned 'After' which states the service that we are going to create must be running first.
  • [Service] Section defines the service type, username, group, what to do in failure, restart timeout. The main is 'ExecStart' which says to start our script file. You can also define 'ExecStartPre' to define anything before the actual script file.'SyslogIdentifier' is the keyword to identify our service in syslog. Similarly, ExecStop is the instruction to say what to do to stop the service.
  • [Install] section is used to define different levels of target in the system.


Next, save the file and start the service using the systemctl command:

$ systemctl start monitor-disk.service

Check the service status using systemctl status command. You can also see service name, file form where systemd service is loaded, documentation, process running and logs:

$ systemctl status monitor-disk.service

Verify that your script is correctly working by looking into the file defined in the script file:

$ cat /var/storage-monitor.txt

You can also check the enable, disable facility of the systemd manager:

$ systemctl enable monitor-disk.service
$ systemctl disable monitor-disk.service

Also, check by stopping and restarting the service. No error should be thrown:

$ systemctl stop monitor-disk.service
$ systemctl restart monitor-disk.service

Systemd also enables default logging in syslog. So, you can view the live log of the service using the following command. Search the keyword 'SyslogIdentifier=Diskutilization' you denied in the above service file:

$ tail -f /var/log/syslog


[Need assistance in solving Systemd Linux errors? We can help you. ]


Conclusion

This article covers how to run your own shell script as a systemd service. Basically, Systemd is a software application that provides an array of system components for Linux operating systems. It is the first service to initialize the boot sequence. This always runs with pid 1. This also helps use to manage system and application service on our Linux operating system.


How to Enable New Service in Linux?

1. To reload the systemctl daemon to read new file, execute:

$ sudo systemctl daemon-reload 

2. To enable the service to start on system boot, also start the service using the following commands:

$ sudo systemctl enable shellscript.service 
$ sudo systemctl start shellscript.service 

3. To verify the script is up and running as a systemd service:

$ sudo systemctl status shellscript.service