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.
In this article, I am going to show you how to create a service using systemd. I will make a manual script which will act like a process to find disk utilization of the Linux system.
Let’s get started. Make a bash script that redirects date and disk utilization in a file. You can create files in any location. Here I am going to make in executable directory /usr/bin
$ sudo vim /usr/bin/script.sh
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
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,
[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
- The [Unit] section consists of description, documentation details. Here I 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.
Just 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
This article shows you how to run your own shell script as a systemd service. We hope you learnt how to make your own service. Please feel free to send your feedback.
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.