run shell script in background

How to Run Shell Script as Background Process

Shell scripts allow you to automate many tasks and processes in Linux. Sometimes you may need to run these shell scripts in background so that your terminal is free for other work. In this article, we will look at how to run shell script as background process.

How to Run Shell Script as Background Process

Here are the steps to run shell script as background process.

1. Create empty shell script

Open terminal and run the following command to create an empty shell script file.

$ sudo vi

2. Add shell commands

Add the following lines to your shell script


echo "Hello World"

Save and quit the file. The above program simply prints “Hello World”.

3. Make shell script executable

Run the following command to make your shell script executable.

$ sudo chmod +x

Run the following command to ensure that your shell script is working.

$ sudo ./
Hello World

4. Run shell script in background

Once you have verified your shell script, you can run it background by simply adding & after your command

$ sudo ./ &

In this case, although the shell script runs in background, it will terminate when you close your shell or terminate your session.

If you are running the command in a terminal and want to close the terminal, then use nohup command to run the script in background.

$ sudo nohup ./ &

If you want to run this shell script as a cronjob, open crontab

$ sudo crontab -e

Add the following line to your crontab file.

0 10 * * * sudo nohup /home/ & >/dev/null 2>&1

In the above command we specify “0 10 * * *” to run the command every day at 10am. We also use the full path of shell script while referring to it in our command. Finally, we direct all output to /dev/null. You may modify it as per your requirement. Save and close the file.

That’s it. As you can see, it is very easy to run shell scripts in background.

Also read:

How to Create Empty Disk Image in Linux
How to Unrar Files With Password in Linux
How to Extract Multipart RAR file in Linux
How to Find Recently Modified Files in Linux
How to Install VNC Server in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *