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
RHEL7 slow ssh login
Viewed 5185 times since Tue, Aug 6, 2019
A tcpdump Tutorial and Primer with Examples
Viewed 6680 times since Sun, Jun 17, 2018
Configuring VLAN interfaces in Linux
Viewed 6852 times since Mon, May 21, 2018
What Is /dev/shm And Its Practical Usage
Viewed 9054 times since Tue, Mar 12, 2019
OpenSSL: Find Out SSL Key Length – Linux Command Line
Viewed 8498 times since Mon, Feb 18, 2019
LVM: Remove an existing Volume Group
Viewed 6174 times since Sat, Jun 2, 2018
OpenSSL: Check SSL Certificate Expiration Date and More
Viewed 7958 times since Mon, Feb 18, 2019
Tunnel SSH Connections Over SSL Using ‘Stunnel’ On Debian 7 / Ubuntu 13.10
Viewed 4296 times since Fri, Sep 28, 2018
HowTo: Create CSR using OpenSSL Without Prompt (Non-Interactive)
Viewed 15085 times since Mon, Feb 18, 2019
ZFS: Verify/change properties of a zfs filesystem
Viewed 3362 times since Sun, Jun 3, 2018