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.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.