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: Route network packets to go out via the same interface they came in
Viewed 4166 times since Sat, Jun 2, 2018
HowTo: Kill TCP Connections in CLOSE_WAIT State
Viewed 17378 times since Thu, Feb 14, 2019
Using renice and taskset to manage process priority and CPU affinity with Linux OEL 6.4
Viewed 4591 times since Mon, Feb 17, 2020
10 Linux rsync Examples to Exclude Files/Directories
Viewed 12230 times since Wed, Oct 31, 2018
How to use yum-cron to automatically update RHEL/CentOS Linux
Viewed 3523 times since Wed, Oct 17, 2018
Yum Update: DB_RUNRECOVERY Fatal error, run database recovery
Viewed 4996 times since Fri, Jan 17, 2020
How to encrypt a partition using LUKS?
Viewed 2597 times since Fri, Jul 13, 2018
linux unix aix banner /etc/issue
Viewed 2613 times since Fri, Aug 3, 2018
HowTo: The Ultimate Logrotate Command Tutorial with 10 Examples
Viewed 6937 times since Fri, Nov 30, 2018
ZPOOL: Remove an existing zpool
Viewed 2962 times since Sun, Jun 3, 2018