Systemd is a system and service manager for Linux operating system which is running as the first process on boot with PID 1 and bring up the entire system as well as maintaining userspace services. Let’s leave the formal descriptions and move on with real facts.
I assume that you have an application that should run on the background(daemon) as soon as the computer boot up. So we know that when you switch on your computer after booting then Systemd will start as the first process, initialize and fire up the rest of the system. Systemd is managing service as units and each service has the corresponding service file.
In this demo, I will use a simple bash script called timetracker.sh which logging the date every 10 seconds. The content of the script looks as below:
$ cat timetracker.sh#/!/bin/bash#infinite loop
echo “Logger is logging the date…”
echo "see in /var/log/syslog"
Now we should make the script executable and put it under /usr/local/bin directory.
$ chmod +x timetracker.sh
$ sudo mv timetracker.sh /usr/local/bin
The script is ready and executable. Now it’s time to create our service file which will be required by Systemd. All service files with <yourservice>.service located under /etc/systemd/system directory where we also have to create our custom service file. We call our service file tracker.service and a typical service file consist of three main parts Unit, Service, and Install. Let’s create it!
$ sudo vim tracker.service
Unit section contains parameters that are not explicitly dependent on the type of the unit. You can provide a description, behavior such as After, Before, etc. For further details see. In our example, I wrote simply This is a simple time logger agent.
Service section you can specify the type of service, the path to the binary file to be executed, and another bunch of options. For more see. I will simply write Type=simple but if your service creates subprocess which will be later on the main process then you have to may use forking as type.
Install section set up unit installation used by systemctl enable command. You may specify some other services which require a dependency on your service. I specified multi-user.target which contains multiple services, see following directory /etc/systemd/system/multi-user.target.wants.
Description= This is a simple time logger agent[Service]
So the tracker.service file is ready. Before reloading daemons and start our service. Commands are executed respectively as below
$ sudo chmod 664 tracker.service #giving permissions
$ sudo systemctl daemon-reload #after editing or creating any file in /etc/systemd/system/ the files have to be reloaded
$ sudo systemct enable --now tracker.service # starting the service, with --now you start and enable service immediately.
$ sudo systemctl status tracker.service # see status of your service
To verify our daemon service, we can check logs:
$ sudo cat /var/log/syslog
So our service is running in background and logging every 10 seconds the current date :)
If you want to stop the service then use systemctl again.
$ sudo systemctl stop tracker.service
So we noticed that Active status is turned to inactive and logs says also stopped the is a simple time logger agent.
Anytime to reactivate your service simply $ sudo systemctl restart tracker.service
If you have any trouble please check logs:) and possible causes
* if you forget shebang in your timetracker.sh script → #!/bin/bash* typos either in the script or in tracker.service file* Do not forget to give the right permission References