Run any Executable as Systemd Service in Linux

Source

What is systemd?

  • 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

$ 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

$ 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)

$ 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

  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

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.

--

--

--

ML Practitioner | Kaggle Master

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

[Solved] How to install Cocoapods on M1 Mac - when “sudo gem install ffi” FAIL

Salesforce vs Bitrix24: Explore a Land of New Opportunities +Video— Data2CRM-2018

How to Switch Virtual Cameras Using Only Cinemachine and Scripting in Unity

Bi-Weekly Progress Report — April 7— April 19, 2020

How to get the body from a Word DOCX document in Java

McKinley Beach Lot 3s5s, Windows Are In

McKinley Beach Lot 3s5s, Windows Are In

Install and configure OpenCV-4.2.0 in Windows 10 — Python

The essence of Concurrent Programing

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

More from Medium

Bash Scripting for auto-commit and push GIT

In this article, we will look at how container systems use UTS namespace to provide hostname…

How To: Install Apache Web Server on CentOS 8 [No Fluff]

I Dislike Ubuntu Snaps | Docker Compose Is My Hero— An Old Blog Post