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.service2. The following is a basic template for creating a systemd service:
[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.targetAfter 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.targetCopy 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-reload4. Start/activate the service.
$ sudo systemctl start test-app.service5. To check if our service is running or not, run.
$ sudo systemctl status test-app.service6. To enable the service to auto-start on system boot, run:
$ sudo systemctl enable test-app.service7. 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.service8. To disable the service from auto-starting on system boot, run:
$ sudo systemctl disable test-app.service9. Stop/deactivate the service.
$ sudo systemctl stop test-app.service10. To remove your custom systemd service, simply delete the file from the β/etc/systemd/systemβ directory.
$ sudo rm /etc/systemd/system/test-app.serviceThatβs it. To learn more about managing systemd services, you can check out our article on systemctl commands.




