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
12 Tcpdump Commands – A Network Sniffer Tool
Viewed 8639 times since Fri, Jul 27, 2018
Oracle Linux 7 – How to audit changes to a trusted file such as /etc/passwd or /etc/shadow
Viewed 2759 times since Wed, Jul 25, 2018
How to automate SSH login with password? ssh autologin
Viewed 2487 times since Fri, Jun 8, 2018
30 Handy Bash Shell Aliases For Linux / Unix / MacOS
Viewed 4330 times since Thu, Feb 11, 2021
Szybkie sprawdzenie zewnętrznego adresu IP i hosta
Viewed 3193 times since Thu, May 24, 2018
Using renice and taskset to manage process priority and CPU affinity with Linux OEL 6.4
Viewed 3399 times since Mon, Feb 17, 2020
RHEL: Extending a multipath LUN
Viewed 4626 times since Sun, May 27, 2018
ubuntu How to reset lost root password on Ubuntu 16.04 Xenial Xerus Linux
Viewed 8075 times since Tue, Dec 8, 2020
linux-training.be gives you books for free to study Linux
Viewed 4704 times since Sat, Jun 2, 2018
Linux – delete the LUN and remove traces from OS
Viewed 3225 times since Tue, May 22, 2018