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:
# Script generates disk utilization by the system and store in a file
date >> /var/storage-monitor.txt
sudo du -sch / >> /var/storage-monitor.txt
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:
Description=My disk monitoring service
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. ]