Service creation in Fedora 33

This short document give directions for the creation of services in a Linux machine with the
Systemd service management (which most distributions use nowadays) so even it is specifically
addressed to Fedora 33 it might be useful for other distributions, too.

This has been written by Juan Domingo, University of Valencia. For questions, send me an email to
J1u2a3n4D5o6m7i8n9go#uv.es
getting rid of the numbers and substituting the hash by the @ symbol. Nevertheless, I will not always be able to answer quickly.

The guide assumes basic knowledge of Linux (console use and bash commands).

From now on, everything should be done with root privileges, so become root (su -) or precede all commands with sudo.

This example creates a service to run something that starts at the beginning and runs forever unless it fails or you
kill it (the typical daemon in Unix). As an example, we will make a service to run with docker a container that
makes of your machine a ShareLaTeX server.

Use your favourite editor to create the file /usr/lib/systemd/system/sharelatex.service with the following content:

[Unit]
Description=ShareLaTeX Service
Requires=docker.service
After=nginx.service

[Service]
Type=simple
WorkingDirectory=/home/SHARELATEX/sharelatex
User=root
ExecStart=/usr/bin/docker-compose -f /home/SHARELATEX/sharelatex/docker-compose.yml up
ExecStop=/home/SHARELATEX/bin/stop_sharelatex.sh

[Install]
WantedBy=multi-user.target 

For any other service you may guess what should be changed. Just in case:

Requires is the list (separated by spaces) of installed services that must have been activated, and succeed, for this service to run.

Similarly,

After is the same type of list, but for services that should have been activated before ours, but if they fail, our service will be activated, anyway.

In our case, sharelatex absolutely needs docker, but it could be activated even without web server.
This would make it probably not very useful (not accesible from outside), but if it runs well we would know that the problem is not in it, but in nginx.

With respect to ExecStart and ExecStop, they are obviously the commands to launch and stop the service
As in this example, they can be either one command with its arguments or, if we need several commands, a shell script we will write now.
Let's give the service configuration file the correct owner and permissions:

chown root:root /usr/lib/systemd/system/sharelatex.service
chmod 0644 /usr/lib/systemd/system/sharelatex.service

Finally, create the following symbolic link:
ln -s /usr/lib/systemd/system/sharelatex.service /etc/systemd/system/sharelatex.service

As said before, if one of the start or stop commands is a script, we must write it. In this case, edit the file /home/SHARELATEX/bin/stop_sharelatex.sh
with the following content:
#!/bin/bash
/usr/bin/docker stop sharelatex
/usr/bin/docker stop redis
/usr/bin/docker stop mongo

and give it appropriate owner and permissions:
chown root:root /home/SHARELATEX/bin/stop_sharelatex.sh
chmod 0755 /home/SHARELATEX/bin/stop_sharelatex.sh

and that's all. The service should be ready to run, and to start after boot if we enable it. Test with
systemctl start sharelatex
systemctl status sharelatex
If something fails, check the log with
journalctl -xe
and if not, enable it:
systemctl enable sharelatex

If you have come here from my former page on ShareLaTeX installation, go back