Running scripts on boot in Linux using systemd

Introduction

In this article, we will configure our Linux computer to autorun a script on boot. For the purpose of this article, we will be making use of systemd services.

BTW, I use Arch Linux so if there’s any difference using a differen…


This content originally appeared on DEV Community and was authored by Kayode

Introduction

In this article, we will configure our Linux computer to autorun a script on boot. For the purpose of this article, we will be making use of systemd services.

BTW, I use Arch Linux so if there’s any difference using a different distro, do not hesitate to leave a comment, please.

linux-when.gif

What is systemd

systemd is a suite of basic building blocks for a Linux system. It provides a system and a service manager that runs as PID and starts the rest of the system. systemd provides aggressive parallelisation capabilities, uses sockets and D-Bus activation for starting services, keep track of processes etc.

You can read more on systemd here.

The main command used to introspect and control systemd is systemctl.

Steps

  • First, we will create a Bash script in /usr/local/sbin, for instance [notifyRemote.sh](http://notifyRemote.sh) that would notify a remote machine once it’s booted.
  • We will make the file executable by running the command:

    sudo chmod +x /usr/local/sbin/notifyRemote.sh
    
  • Create a Unit file called startup.service in /etc/systemd/system/ to define a systemd service. You would need root access (sudo) to make changes or create these files.

  • We will make the file executable by running the command:

    sudo chmod +x /etc/systemd/system/startup.service
    
  • In /etc/systemd/system/startup.service we would paste the below into the file as such:

    [Unit]
    Description=My Startup
    
    [Service]
    ExecStart=/usr/local/sbin/notifyRemote.sh
    
    [Install]
    WantedBy=multi.user.target
    
  • The ExecStart is the most important key here because it points to the Bash program that will be run when the service is started.

  • We can test the service by running sudo systemctl start startup.service to confirm that the script will run

  • Now to enable services to run on boot, we will run the command

    sudo systemctl enable startup.service
    

Conclusion

Now we should successfully create a script that runs automatically anytime we start our Linux machine.

You can consult systemd man page for more information.

I hope you find this helpful. Please leave a like, comment and share if you find this helpful and also you can consider buying me a coffee too.


This content originally appeared on DEV Community and was authored by Kayode


Print Share Comment Cite Upload Translate Updates
APA

Kayode | Sciencx (2022-04-09T00:12:42+00:00) Running scripts on boot in Linux using systemd. Retrieved from https://www.scien.cx/2022/04/09/running-scripts-on-boot-in-linux-using-systemd/

MLA
" » Running scripts on boot in Linux using systemd." Kayode | Sciencx - Saturday April 9, 2022, https://www.scien.cx/2022/04/09/running-scripts-on-boot-in-linux-using-systemd/
HARVARD
Kayode | Sciencx Saturday April 9, 2022 » Running scripts on boot in Linux using systemd., viewed ,<https://www.scien.cx/2022/04/09/running-scripts-on-boot-in-linux-using-systemd/>
VANCOUVER
Kayode | Sciencx - » Running scripts on boot in Linux using systemd. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/09/running-scripts-on-boot-in-linux-using-systemd/
CHICAGO
" » Running scripts on boot in Linux using systemd." Kayode | Sciencx - Accessed . https://www.scien.cx/2022/04/09/running-scripts-on-boot-in-linux-using-systemd/
IEEE
" » Running scripts on boot in Linux using systemd." Kayode | Sciencx [Online]. Available: https://www.scien.cx/2022/04/09/running-scripts-on-boot-in-linux-using-systemd/. [Accessed: ]
rf:citation
» Running scripts on boot in Linux using systemd | Kayode | Sciencx | https://www.scien.cx/2022/04/09/running-scripts-on-boot-in-linux-using-systemd/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.