Create a Background Service Using systemd In Linux
First of all, we are going to write a small python script which print "Hello World" every 60 seconds. This is going to be our service script (hello_world.py):
You can execute it by python hello_world.py. If you get boring reading so many hello worlds, press Ctrl+C (or Cmd+C on OSX) to stop it. Save this file as hello_world.py in your home folder (home/pi/). Now we're going to define the service to run this script:
The service definition must be on the /lib/systemd/system folder. Our service is going to be called "hello.service":
Here we are creating a very simple service that runs our hello_world script and if by any means is aborted is going to be restarted automatically. You can check more on service's options in the next wiki: https://wiki.archlinux.org/index.php/systemd.
Now that we have our service we need to activate it:
For every change that we do on the /lib/systemd/system folder we need to execute a daemon-reload (third line of previous code). If we want to check the status of our service, you can execute:
In general:
Comments
Post a Comment