What is systemd?
systemd is new software suite from RedHat to unify service management in Linux distributions. System services like ssh, network and also custom services like web servers can be managed by systemd.

So, we are going to create a systemd service definition for a program to control it more easily.

Create a program to run as a service

We can create a mock program to run as a service or use a program already have. I’ll create a bash script named my-service.sh

#!/bin/bashwhile true
do
echo The current time is $(date)
sleep 1
done

set mode to executable

chmod +x my-service.sh

Test your service:

> ./my-service.shCurrent time is Sat Jan 26 13:23:01 UTC 2019
Current time is Sat Jan 26 13:23:02 UTC 2019
Current time is Sat Jan 26 13:23:03 UTC 2019
Current time is Sat Jan 26 13:23:04 UTC 2019

Create service definition

Now we have a program that can potentially run as a service on Linux machine. We need to define how the service will behave.

systemd stores service definitions in /etc/systemd folder. Go to /etc/systemd/system and create a file named my-service.service

[Service]
ExecStart=/home/yortuc/my-service.sh

Here we define our program to run as service. After saving the file you can start your brand new service.

> sudo systemctl start my-service

And check the status of service

> sudo systemctl status my-service● my-service.service
Loaded: loaded (/etc/systemd/system/my-service.service; static; vendor preset: enabled)
Active: active (running) since Sat 2019-01-26 13:27:23 UTC; 6s ago
Main PID: 2003 (my-service.sh)
Tasks: 2 (limit: 1135)
CGroup: /system.slice/my-service.service
├─2003 /bin/bash /home/vagrant/my-service.sh
└─2027 sleep 1
Jan 26 13:27:23 vagrant systemd[1]: Started my-service.service.
Jan 26 13:27:23 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:27:23 UTC 2019
Jan 26 13:27:24 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:27:24 UTC 2019
Jan 26 13:27:25 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:27:25 UTC 2019
Jan 26 13:27:26 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:27:26 UTC 2019
Jan 26 13:27:27 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:27:27 UTC 2019
Jan 26 13:27:28 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:27:28 UTC 2019
Jan 26 13:27:29 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:27:29 UTC 2019

You can see that our service is running with PID 2003. Also you can see last couple of logs our service created.

Checking logs from service

Usually we don’t see all logs, because by default, service logs goes to syslog folder. So you can check system logs anytime with tail command.

> tail /var/log/syslogJan 26 13:32:02 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:32:02 UTC 2019
Jan 26 13:32:03 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:32:03 UTC 2019
Jan 26 13:32:04 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:32:04 UTC 2019
Jan 26 13:32:05 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:32:05 UTC 2019
Jan 26 13:32:06 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:32:06 UTC 2019
Jan 26 13:32:07 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:32:07 UTC 2019
Jan 26 13:32:08 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:32:08 UTC 2019
Jan 26 13:32:09 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:32:09 UTC 2019
Jan 26 13:32:10 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:32:10 UTC 2019
Jan 26 13:32:11 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:32:11 UTC 2019

Or, if you want to see the logs in real time specifically created by our service, you can use journalctl command.

> sudo journalctl -u my-service -fJan 26 13:33:31 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:33:31 UTC 2019
Jan 26 13:33:32 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:33:32 UTC 2019
Jan 26 13:33:33 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:33:33 UTC 2019
Jan 26 13:33:34 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:33:34 UTC 2019
Jan 26 13:33:35 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:33:35 UTC 2019
Jan 26 13:33:36 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:33:36 UTC 2019
Jan 26 13:33:37 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:33:37 UTC 2019
Jan 26 13:33:38 vagrant my-service.sh[2003]: Current time is Sat Jan 26 13:33:38 UTC 2019
...

And finally, you can stop the service

> sudo systemctl stop my-service

Service definition is much more powerful than just defining only ExecStart. You can define environments, what should be run before and after your service starts, what to do if service fails, customizing stoping mechanism if your service needs and so on. You can refer to the RedHat documentation: https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/sect-managing_services_with_systemd-unit_files

Happy hacking.

Data engineer @ ANSYS