Linux 101 : Tweaking the "Systemd" Init files



In some circumstances, we might need to tweak the systemd init script - startup script - of a systemd service.

Below is a simple example of a startup script of a systemd service called "simple-script":


The above script represents a startup file a systemd service "simple-script".
The parameter "Type=oneshot" means that the program executes until it finishes and exits. 
Systemd waits for the program to exit. It suits the processes that do a "job" and exit.

The "wantedby" simply means that this service needs to be started when we move to the multi-user.target "target" or "run-level".

To make the script run at boot time, we will need to enable it using the below command:


Sometimes the oneshot program is needed as a dependency for other services. 
In that case we can combine the "Type=oneshot" feature with the "RemainAfterExit=yes" feature that tells systemd to keep the oneshot program as "active" even after it finishes its task.

We could see an example of that by running the below command:


Below is a "Oneshot" and "RemainAfterExit" example:


Below is an explanation of some of the parameters used:
  • ExecStart : the command that starts our program.
  • ExecStop : the command that is used to stop the program.
Remark:

Re-running the above init script will not start anything.

Sending notification about the successful "start" of a program, might be useful in same cases. 
We could do that using the parameter "notify" as you can see below:


For this kind of services, systemd will "kill" the service if it doesn't receive any "success notification" after a certain period - timeout -.

Below is a short description of the most used parameter in the init script files:
  • ExecStartPre : represents the command to be run before the ExecStart command.
  • ExecStopPost : represents the command to be run after the ExecStop command.
  • ExecStartPost : represents the command to be run after the ExecStart commands have finished
  • ExecStopPost : represents the command to be run me after the ExecStop command.
  • RestartSec : waiting time before a service is restarted.

Comments

Leave as a comment:

Archive