How to create a Systemd service in Linux

At times you create a script and then you want to have the scripts controlled by systemd or in some cases you wish to have the scripts getting restarted by itself when it is killed due to some reason. In such cases systemd in Linux helps to configure services which can be managed. To do so follow the following steps.

  1. cd /etc/systemd/system
  2. Create a file named your-service.service and include the following:

    [Unit]
    Description=<description about this service>
    
    [Service]
    User=<user e.g. root>
    WorkingDirectory=<directory_of_script e.g. /root>
    ExecStart=<script which needs to be executed>
    Restart=always
    
    [Install]
    WantedBy=multi-user.target

     

  3. Reload the service files to include the new service.
    sudo systemctl daemon-reload


  4. Start your service
    sudo systemctl start your-service.service

  5. To check the status of your service
    sudo systemctl status example.service

  6. To enable your service on every reboot
    sudo systemctl enable example.service

  7. To disable your service on every reboot
    sudo systemctl disable example.service

0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
LVM: Create a new Logical Volume / Filesystem
Viewed 1042 times since Sat, Jun 2, 2018
RHCS6: Quorum disk and heuristics
Viewed 1828 times since Sun, Jun 3, 2018
List DNS records, nameservers of a domain from command line
Viewed 720 times since Sun, Sep 30, 2018
ZPOOL: Create a new zpool for zfs filesystems
Viewed 1019 times since Sun, Jun 3, 2018
Easily Find Bugs In Shell Scripts With ShellCheck
Viewed 1029 times since Thu, Apr 18, 2019
Learn Linux System Auditing with Auditd Tool on CentOS/RHEL
Viewed 1095 times since Fri, Apr 5, 2019
RHCS6: Luci - the cluster management console
Viewed 1321 times since Sun, Jun 3, 2018
Linux 16 Useful Bandwidth Monitoring Tools to Analyze Network Usage in Linux
Viewed 905 times since Mon, Sep 21, 2020
Linux Chage Command to Set Password Aging for User
Viewed 702 times since Tue, Sep 11, 2018
Linux – Securing your important files with XFS extendend attributes
Viewed 824 times since Wed, Jul 25, 2018