In this post you will learn how to convert your headless Linux program to a service running in the background.
Here with headless is meant a program without user interaction and without graphical user interface (GUI).
Assumed is that you are running a Linux distribution with Systemd enabled, like Ubuntu 15.10.
In the example below, the name of the service is ‘my-service‘ and the executable is named: ‘/opt/bin/MyProgram‘. Of course you can change these to your own needs.
First create a file:
/etc/systemd/system/my-service.service
.. with these contents:
[Unit] Description=MyService [Service] Type=simple Restart=always RestartSec=30 UMask=022 ExecStart=/opt/bin/MyProgram [Install] WantedBy=multi-user.target
After creating the file, let the systemd-daemon restart to read the new configuration file:
sudo systemctl daemon-reload
Now you are ready to start your new service:
sudo systemctl start my-service
To check the status of the service, run the command:
sudo systemctl status my-service
When everything is ok, make sure the service will be started at the next boot:
ln -s /etc/systemd/system/my-service.service /etc/systemd/system/multi-user.target.wants/my-service.service