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
debian Debian/Ubuntu Linux: Find If Installed APT Package Includes a Fix/Patch Via CVE Number
Viewed 7133 times since Sun, Sep 23, 2018
Method 2 – Use shell scripts How to install yum cron on a CentOS/RHEL 6.x/7.x
Viewed 1256 times since Tue, Dec 4, 2018
RHCS6: Install a two-node basic cluster
Viewed 1407 times since Sun, Jun 3, 2018
RHEL: Services basic management - systemd
Viewed 15978 times since Sat, Jun 2, 2018
Secure Remote Logging to Central Log Server Using RSYSLOG on CentOS 6 / CentOS 7 and stunnel
Viewed 1112 times since Sun, Dec 6, 2020
What is yum-cron ?
Viewed 1712 times since Fri, Oct 26, 2018
Linux Customizing Bash
Viewed 441 times since Sun, Dec 6, 2020
LVM: Move allocated PE between Physical Volumes
Viewed 1851 times since Sat, Jun 2, 2018
RHEL: Manually encrypting a filesystem with LUKS
Viewed 1994 times since Sun, May 27, 2018
Linux: Disks diagnostic using smartctl
Viewed 11992 times since Wed, Jul 25, 2018