import Script from 'next/script';

How to create service using Systemd on Linux

Author: Aman Kulshrestha

Written on: April 11, 2025

Systemd on Linux

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:

  1. Open the terminal and run:
    sudo nano /etc/systemd/system/myservice.service
    This will create and open the file myservice.service (you can use any name with a .service extension).
  2. 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
  3. Reload the systemd daemon:
     sudo systemctl daemon-reexec 
     sudo systemctl daemon-reload

Summary Table

StepCommand/ActionDescription
1sudo nano /etc/systemd/system/myservice.serviceCreate and open the service file.
2Add service configuration.Define the service details in the file.
3sudo systemctl daemon-reexec
sudo systemctl daemon-reload
Reload the systemd daemon to apply changes.
4sudo systemctl status myserviceCheck the status of the service.
5journalctl -u myservice -fView 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.