Linux Commands

How to Run Shell Script at Startup

To perform some specific tasks at the startup of the system, shell scripts are used. We can do some great customized things at the startup of the system with the help of scripts, such as showing welcome messages, showing the system version, or running a program/application on the startup. This write-up will provide an explanation of how to run a shell script at a startup.

How to Run Shell Script at Startup

To run the shell script at the startup of the system, we will follow the below-given methods:

So, let’s explore each method one by one!

Method 1: Use Cron Job to Run Script at Startup

Cron job usually assist in fulfilling different tasks which are needed to be completed in the future. It simply automates things for users. Cron job can also be utilized in running scripts at the startup.  To do so, let’s begin with creating a bash script that will show the welcome message to the users at the startup of the system:

First create a file using:

$ sudo nano Testscript.sh

 
And type the script:

#!/bin/bash
echo "Hello Sam" >> /home/kash/test.txt

 

After writing code press “Ctrl + O” to write out, then “Enter” to save file name. Now press “Ctrl + X” to exit.

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

$ chmod a+x Testscript.sh

 

Now, open the crontab file by executing the following command and enter the password to continue:

$ sudo crontab -e

 

Consider choosing the editor which seems suitable & easiest to you. As nano is the simplest one, write “1” to edit the crontab file in it.


In the crontab file, scroll to the end and add the following code to run the script on every startup:

@reboot bash /home/kash/Testscript.sh

 

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


Now simply reboot the system to verify if the script is running on the startup or not. On every reboot the following text will be printed.

Method 2: Use Startup Application Process

On Ubuntu and most distributions have a default tool that allows for running a specific script at startup. This method works if you have GNOME installed in your system. Follow the given steps to setup the startup script using Startup Application.

Create a bash script using cat command.

$ nano Testscript.sh

 

Now we will write a bash script which will run every time the system is rebooted and print the text “Hello Sam” in the test.txt file.

#!/bin/bash
echo "Hello Sam" >> /home/kash/test.txt

 

After writing code press “Ctrl + O” to write out, then “Enter” to save file name. Now press “Ctrl + X” to exit.

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

$ chmod a+x Testscript.sh

 

Now create a text file using the nano editor.

$ nano test.txt

 

Open the startup menu and select “Startup Application Preferences”.


After clicking on Startup Applications Preferences, you will get a list of tasks that are configured to start automatically on boot.

Using the “Add” button any program can be added in the list to start it automatically at startup.


Select the .sh file and click save.


Now reboot the system. Text will be printed every time the system is restarted or rebooted.

Conclusion

Running shell scripts at startup in Linux allows users to execute specific tasks at startup. To run script at startup permission must be given to the script file. Two methods using which a script can be run at startup includes the Cron job and startup applications. For more description of both steps read the given article.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.