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
How log rotation works with logrotate
Viewed 5067 times since Fri, Nov 30, 2018
How to run command or code in parallel in bash shell under Linux or Unix
Viewed 3429 times since Tue, Aug 6, 2019
10 Linux cryptsetup Examples for LUKS Key Management (How to Add, Remove, Change, Reset LUKS encryption Key)
Viewed 5387 times since Tue, Jul 31, 2018
Super Grub2 Disk
Viewed 3564 times since Wed, May 22, 2019
List of 10 Must Know Oracle Database Parameters for Database Administrator
Viewed 131144 times since Thu, Jun 21, 2018
How to enable Proxy Settings for Yum Command on RHEL / CentOS Servers
Viewed 12846 times since Thu, Jul 19, 2018
RHCS6: Extend an existing Logical Volume / GFS2 filesystem
Viewed 3430 times since Sun, Jun 3, 2018
Inxi: Find System And Hardware Information On Linux
Viewed 2469 times since Sat, Jun 2, 2018
socat: Linux / UNIX TCP Port Forwarder
Viewed 9864 times since Tue, Aug 6, 2019
LUKS List available methods of encryption for LUKS
Viewed 3119 times since Fri, Jul 13, 2018