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
RHEL: Display swap/RAM size
Viewed 3510 times since Sat, Jun 2, 2018
RHCS6: Reduce a Global Filesystem 2 (GFS2)
Viewed 3579 times since Sun, Jun 3, 2018
10 Linux DIG Command Examples for DNS Lookup
Viewed 11301 times since Sun, Sep 30, 2018
How to stop and disable auditd on RHEL 7
Viewed 40742 times since Tue, Aug 6, 2019
How to Clear RAM Memory Cache, Buffer and Swap Space on Linux
Viewed 2954 times since Mon, Nov 23, 2020
Increase A VMware Disk Size (VMDK) Formatted As Linux LVM without rebooting
Viewed 15689 times since Wed, May 30, 2018
Top 25 Best Linux Performance Monitoring and Debugging Tools
Viewed 7273 times since Sun, Sep 30, 2018
How to Configure ‘FirewallD’ in RHEL/CentOS 7 and Fedora 21
Viewed 10733 times since Wed, Oct 9, 2019
LVM: Mount LVM Partition(s) in Rescue Mode
Viewed 5092 times since Sat, Jun 2, 2018
LVM basic
Viewed 2386 times since Sat, Jun 2, 2018