How to use systemd to have your apps running in the background forever
If you want your apps to run forever, that is:
- run when the program crashes
- starts when the operating system starts
then you'd simply create a new systemd unit. To do that, we'll create a systemd unit file.
For a service named "foobar", we will create it in
And the content would look like so:
[Unit]Description=Does somethingAfter=network.target[Service]Type=simpleUser=root# Change the following two configsWorkingDirectory=/the/working/directoryExecStart=/path/to/programRestart=always[Install]WantedBy=multi-user.target
Then restart the systemctl daemon.
Then, to run your app as a service, invoke the following command.
sudo systemctl start foobar
And then, to ensure that it starts on boot, invoke the following command.
sudo systemctl enable foobar
Whether things went well or not, you should still see absolutely nothing.
If your application is a web server, you can ping it with a cURL request, to see if it's working.
If it isn't, then you can take a look at the logs to see what may have gone wrong.
Invoke the following command:
sudo journalctl -xe
Hopefully you should have a good idea on how to get things working.
Digital ocean has a comprehensive article on what systemd is, and how you can take full advantage of it.