How to Create a Systemd Service in Linux (under 1 Minute)

Linux TLDR
Last Updated:
Reading time: 2 minutes

Systemd is a popular init system used by many major Linux distributions, such as Ubuntu, Debian, Red Hat, and Fedora. It’s ofter a service manager that manages various programs and processes on your Linux system. The systemd services are defined in unit files.

Few programs create a unit file during installation; for example, when installing Apache or Nginx, they add a unit file to your systemd directory for easier management. However, you can also create your own custom systemd service in Linux by creating a new unit file.

The new systemd service can be used to run a shell script repetitively, back up a system at regular intervals, monitor certain directories, manage specific services, and much more.

Create Custom Systemd Service File in Linux

For demonstration, I’ll create a custom systemd service file for a shell script located at β€œ/home/linuxtldr/myscriptβ€œ, so be sure to change this path to your desired application or shell script.

1. All the systemd service (or unit) files reside at the β€œ/etc/systemd/system” path, so create a new file for your systemd service, which I’ve named β€œtest-appβ€œ.

$ sudo nano /etc/systemd/system/test-app.service

2. The following is a basic template for creating a systemd service:

πŸ“
To get a complete list of all systemd service options with detailed examples, click here.
[Unit]
Description=<description about this service>

[Service]
User=<user responsible for running the script>
Group=<group responsible for running the script>
WorkingDirectory=<absolute directory path of the script>
ExecStart=<absolute path of the script>
# Optional items below
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

After applying all the changes in the above systemd service template to run my shell script, it will look like this:

[Unit]
Description=Running a shell script

[Service]
User=root
Group=root
WorkingDirectory=/home/linuxtldr/
ExecStart=/home/linuxtldr/myscript
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

Copy and paste the above content into β€œtest-app.serviceβ€œ, then save and close the file.

3. Reload the systemd services to include our new service.

$ sudo systemctl daemon-reload

4. Start/activate the service.

$ sudo systemctl start test-app.service

5. To check if our service is running or not, run.

$ sudo systemctl status test-app.service

6. To enable the service to auto-start on system boot, run:

$ sudo systemctl enable test-app.service

7. Verify that the service is successfully configured to auto-start during system boot by checking the output of the following command (where β€œenable” signifies automatic startup on boot and β€œdisable” signifies no automatic startup on boot):

$ sudo systemctl is-enabled test-app.service

8. To disable the service from auto-starting on system boot, run:

$ sudo systemctl disable test-app.service

9. Stop/deactivate the service.

$ sudo systemctl stop test-app.service

10. To remove your custom systemd service, simply delete the file from the β€œ/etc/systemd/system” directory.

$ sudo rm /etc/systemd/system/test-app.service

That’s it. To learn more about managing systemd services, you can check out our article on systemctl commands.