Bootstrap FreeKB - Linux Fundamentals - Systemd service files
Linux Fundamentals - Systemd service files

Updated:   |  Linux Fundamentals articles

On a systemd system, the /etc/systemd/system directory contains files and sub directories that are used to manage services. Let's say you want to create a service called foo. Create the foo.service file.

touch /etc/systemd/system/foo.service

 

The file can then be configured to start and stop the foo service. In this example, there is a shell script (foo.sh) that is used to start or stop the foo service.

[Unit]
Description=foo service
After=network.target

[Service]
Type=forking
PIDFile=/path/to/foo.pid
ExecStart=/path/to/foo.sh start
ExecStop=/path/to/foo.sh stop

[Install]
WantedBy=multi-user.target

 

Reload the daemons, so that systemctl knows about foo.service.

systemctl daemon-reload

 

Start and enable the service. Now, when the system is reboot, foo.service will invoke foo.sh start to start the foo service.

systemctl start foo.service
systemctl enable foo.service

 

ExecStartPre, ExecStart and ExecStop will return FAILURE when the command in question returns an exit code other than 0, like this.

Process: 12345 ExecStart=some bogus command (code=exited, status=1/FAILURE)

 

There are scenarios where you want to continue execution even when a command returns an exit code other than 0. In these scenarios, the - character can be used, like this:

ExecStart=-/path/to/foo.sh start

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 9941ea in the box below so that we can be sure you are a human.