How to Run Shell Script at Startup

4

To participate in some unique duties on the startup of the system, shell scripts are used. We can do some outstanding custom-made matters on the startup of the system with the assistance of scripts, akin to exhibiting welcome messages, exhibiting the system variation, or operating a program/software on the startup. This write-up will furnish an rationalization of the way to run a shell script at a startup.

How to Run Shell Script at Startup

To run the shell script on the startup of the system, we’ll comply with the below-given strategies:

So, let’s discover every system one after the other!

Method 1: Use Cron Job to Run Script at Startup

Cron job more often than not help in fulfilling completely different duties that are necessary to be accomplished in the long run. It in simple terms automates matters for customers. Cron job may also be utilized in operating scripts on the startup.  To accomplish that, let’s start with making a bash script that may exhibit the welcome message to the customers on the startup of the system:

First create a file utilizing:

$ sudo nano Testscript.sh

And sort the script:

#!/bin/bash
echo “Hello Sam” >> /dwelling/kash/take a look at.txt

After writing code press “Ctrl + O” to put in writing out, then “Enter” to avoid wasting file identify. Now press “Ctrl + X” to exit.

To perform the bash script file executable run the given command.

$ chmod a+x Testscript.sh

Now, open the crontab file by executing the subsequent command and enter the password to proceed:


Consider determining the editor which appears acceptable & simplest to you. As nano is the only one, write “1” to edit the crontab file in it.


In the crontab file, scroll to the tip and add the subsequent code to run the script on each and every startup:

@reboot bash /dwelling/kash/Testscript.sh


Press “Ctrl+S” to avoid wasting and “Ctrl+X” to exit from the editor.


Now in simple terms reboot the system to confirm if the script is operating on the startup or not. On each and every reboot the subsequent textual content might be printed.

Method 2: Use Startup Application Process

On Ubuntu and most distributions have a default software that enables for operating a unique script at startup. This system works if you could have GNOME established in your system. Follow the given steps to setup the startup script utilizing Startup Application.

Create a bash script utilizing cat command.

Now we’ll write a bash script which can run each time the system is rebooted and print the textual content “Hello Sam” within the take a look at.txt file.

#!/bin/bash
echo “Hello Sam” >> /dwelling/kash/take a look at.txt

After writing code press “Ctrl + O” to put in writing out, then “Enter” to avoid wasting file identify. Now press “Ctrl + X” to exit.

To perform the bash script file executable run the given command.

$ chmod a+x Testscript.sh

Now create a textual content file utilizing the nano editor.

Open the startup menu and pick out “Startup Application Preferences”.


After clicking on Startup Applications Preferences, you’re going to get a record of duties which are configured to start off robotically on boot.

Using the “Add” button any program could be added within the record to start off it robotically at startup.


Select the .sh file and click on save.


Now reboot the system. Text might be printed each time the system is restarted or rebooted.

Conclusion

Running shell scripts at startup in Linux enables customers to execute unique duties at startup. To run script at startup permission have got to be given to the script file. Two strategies utilizing which a script could be run at startup comprises the Cron job and startup purposes. For greater description of equally steps study the given article.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More