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

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

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |