Run any Executable as Systemd Service in Linux

Source

What is systemd?

Systemd is a system manager and initialization tool that has become widely popular in recent years. Systemd is also the default init system in most of the well known Linux distributions. So knowing how to create a service in systemd might not be all that bad after all. Some of the popular distros that run on systemd by default include,

  • Arch Linux
  • Debian Jesse or newer
  • CentOS 7 / RHEL 7 or newer
  • Ubuntu 16.04 or newer
  • Fedora 15 or newer
$ systemctl list-units --type=service
systemctl list-units example
$ systemctl status postgresql
$ systemctl cat postgresql

Creating our own systemd service

The magical file that lets us create a systemd service is called a Unit file. I say magical because it is much easier to write and setup. For a functioning service, this file must be present only in the following directories

$ man systemd.unit
Example from systemd manual
$ touch echo-server.service
[Unit]
Description=Service that keeps running the echo-server from startup.
[Install]
WantedBy=multi-user.target
After=network.target
[Service]
Type=simple
ExecStart=/home/abhi/Dev/echo-server/server
WorkingDirectory=/home/abhi/Dev/echo-server
Restart=always
RestartSec=5
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=%n
Type=simple
WorkingDirectory=/home/abhi/Dev/echo-server
Restart=always
RestartSec=5
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=%n

Running the Service and monitoring it

Now since the unit is created, we can move it to the /etc/systemd/system directory.

$ sudo systemctl daemon-reload
$ sudo systemctl enable echo-server.service
$ sudo systemctl start echo-server.service
$ sudo systemctl status echo-server.service
$ journalctl -f -u echo-server.service

Run a shell script as service (Alternative)

This method can be useful when you do not have a standalone binary executable for your application. First, create a shell script that invokes your program. In this case, we should write the python command that executes our program.

$ touch start-echo-server.sh
#!/bin/bashSCRIPT_PATH=/home/abhi/Dev/echo-server/server.py
PYTHON_PATH=/home/abhi/.local/bin/.virtualenvs/main/bin/python
$PYTHON_PATH $SCRIPT_PATHexit 1
$ which python
$ chmod +x start-echo-server.sh
$ sudo mv start-echo-server.sh /usr/sbin
Example

Removing the systemd service

Now let's see how to remove the service completely from the system.

  1. Stop the service if it is running
$ systemctl stop echo-server.service
$ systemctl disable echo-server.service
$ sudo rm /etc/systemd/system/echo-server.service
$ systemctl daemon-reload
$ sudo rm /usr/sbin/start-echo-server.sh
$ systemctl reset-failed

Conclusion

Being able to create these services can be really helpful in many cases, this is just one example where I’ve shown you how to do this but this method can be adopted for several problems where systemd services can do the job elegantly most of the times without involving any third-party tools.

Thanks a ton for reading all the way. If you like the article feel free to hit that clap icon which motivates me to write more good articles.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhinand

Abhinand

ML Practitioner | Kaggle Master