Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Tutorials»How to Run Shell Script as SystemD Service in Linux

    How to Run Shell Script as SystemD Service in Linux

    RahulBy RahulApril 12, 20212 Mins ReadUpdated:May 28, 2021

    Systemd is a software application that provides an array of system components for Linux operating systems. It is the first service to initialize the boot sequence. This always runs with pid 1. This also helps use to manage system and application service on our Linux operating system.

    We can also run any custom script as systemd service. It helps the script to start on system boot. This can be helpful for you to run any script which required to run at boot time only or to run always.

    In our previous tutorial we have provides you instructions to run a Python script using Systemd. This tutorial coverts to run a shell script as Systemd service.

    Step 1 – Create a Shell Script

    First of all, create a sample shell script to run always until the system is running. We will use bash while loop to run infinitely.

    sudo nano /usr/bin/script.sh 
    

    Add the following sample script.

    1
    2
    3
    4
    5
    6
    7
    #!/bin/bash
     
    while true
    do
    // Your statements goes here
    sleep 10
    done

    Save script and set execute permission.

    sudo chmod +x /usr/bin/script.sh 
    

    To run a script once during system boot time doesn’t required any infinite loop. Instead of the above script, you can use your shell script to run as Systemd service.

    Step 2 – Create A SystemD File

    Next, create a service file for the systemd on your system. This file must have .service extension and saved under the under /lib/systemd/system/ directory

    sudo nano /lib/systemd/system/shellscript.service 
    

    Now, add the following content and update the script filename and location. You can also change the description of the service.

    [Unit]
    Description=My Shell Script
    
    [Service]
    ExecStart=/usr/bin/script.sh
    
    [Install]
    WantedBy=multi-user.target
    

    Save the file and close it.

    Step 3 – Enable New Service

    Your system service has been added to your service. Let’s reload the systemctl daemon to read new file. You need to reload this deamon each time after making any changes in in .service file.

    sudo systemctl daemon-reload 
    

    Now enable the service to start on system boot, also start the service using the following commands.

    sudo systemctl enable shellscript.service 
    sudo systemctl start shellscript.service 
    

    Finally verify the script is up and running as a systemd service.

    sudo systemctl status shellscript.service 
    

    Output looks like below:

    Running shell script as systemd service

    Conclusion

    This tutorial helped you to configure a shell script as systemd service.

    shell script systemd
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleShell Script – Remove Double Quote (“”) from a String
    Next Article How to Install netstat Command in Linux

    Related Posts

    What is the /etc/aliases file

    2 Mins Read

    What is the /etc/nsswitch.conf file in Linux

    2 Mins Read

    How to Setup Squid Proxy Server on Ubuntu and Debian

    Updated:June 17, 20225 Mins Read

    How to Delete a Let’s Encrypt Certificate using Certbot

    Updated:June 3, 20222 Mins Read

    Backup MySQL Databases to Amazon S3 (Shell Script)

    Updated:May 28, 20222 Mins Read

    How to Install Latest Git on Ubuntu 22.04

    Updated:May 31, 20222 Mins Read

    6 Comments

    1. Navi on April 9, 2022 10:55 am

      many Thanks .. It is Awsum

      Reply
    2. David Greaves on September 15, 2021 8:50 am

      This is useful material but you should place locally added services in /etc/systemd/system/
      Other paths will work but the /usr/lib (or /lib) on is for the distribution package manager

      https://www.freedesktop.org/software/systemd/man/systemd.unit.html#Unit%20File%20Load%20Path

      A minor detail but it’s always good to be as correct as possible when teaching other people.

      Reply
    3. William on August 25, 2021 3:09 pm

      Thanks you for this great tutorial

      Reply
    4. Raj on August 19, 2021 10:01 am

      Thanks for the steps.. it really helped

      Reply
    5. aditya on June 24, 2021 11:26 am

      can we write a shell script which automatically copies the information to the service file (shellscript.service ) automatically when we run the .sh file instead of us entering it manually ?

      Reply
      • Tarron on June 29, 2021 7:04 am

        of course you can. but this isn’t a bash tutorial

        Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How To Install Docker on Ubuntu 22.04
    • How to Install Bower on Ubuntu 22.04 & 20.04
    • How to run “npm start” through Docker
    • Filesystem Hierarchy Structure (FHS) in Linux
    • How to accept user input in Python
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.