How to create service using Systemd on Linux
Author: Aman Kulshrestha
Written on: April 11, 2025

Systemd is a system and service manager for Linux. It is the first process on boot (with PID = 1) that starts with the OS, and this process begins all other processes or services.
You can create and manage your own services using the systemctl
command.
How to Create a Service in Systemd on Linux?
To create a service, you need to create a .service
file in the following directory:
/etc/systemd/system/myservice.service
Follow these steps to create your service file:
- Open the terminal and run:This will create and open the file
sudo nano /etc/systemd/system/myservice.service
myservice.service
(you can use any name with a.service
extension). - Add the following content to the file:
[Unit] Description=My Custom Service After=network.target [Service] ExecStart=/usr/bin/python3/path/to/your/script.py WorkingDirectory=/path/to/your/ Restart=always User=yourusername Environment=PYTHONUNBUFFERED=1 [Install] WantedBy=multi-user.target
- Reload the systemd daemon:
sudo systemctl daemon-reexec sudo systemctl daemon-reload
Summary Table
Step | Command/Action | Description |
---|---|---|
1 | sudo nano /etc/systemd/system/myservice.service | Create and open the service file. |
2 | Add service configuration. | Define the service details in the file. |
3 | sudo systemctl daemon-reexec sudo systemctl daemon-reload | Reload the systemd daemon to apply changes. |
4 | sudo systemctl status myservice | Check the status of the service. |
5 | journalctl -u myservice -f | View logs for troubleshooting. |
Test & Logs
If the service fails, check the logs using:
journalctl -u myservice -f
Ever wondered how a server recovers from runtime errors and restarts automatically? It is the systemd service file that handles such scenarios.