How to run a bash script in background in Linux

Endrit Qerreti

Endrit Qerreti

Cronjob does a great job when it comes to running a task anytime you want. However, running a bash script as a background process in linux can be done without using cron.

We can use the nohup command, which allows us to start a bash script from the terminal, which then this script will be running as normal process in your system, the good thing about nohup is that the script will continue running even if you close the terminal from where you launched the script.

Another way of running a script in background is using the logical operator & in bash.

We'll explain both ways of running a bash script in background below

Run a bash script in background using nohup

In this method, we are going to use the no hang up command (nohup), which allows the bash script to run in background even after closing the terminal.

nohup myapp.sh &

The bash script that we are running in background is called myapp.sh, replace this with the bash script that you want to run in background, and press enter.

Run a bash script in background using &

To run a bash script in background by using the and command &, assign the & to the bash script that you will be running as show in the example below

./myapp.sh &

Once you press enter, myapp.sh will be launched and will start running in background. The difference between, nohup and & is that nohup doesn't kill the process when you close the shell, like the and command does.

So, if you want your script to run even after closing the terminal, use the nohup command. If you want the script to run for as long as you keep the terminal open, then use the & command.

Run multiple bash scripts in background

If you have multiple bash scripts that you need to run after one script, you can use the && command. This command allows you to run multiple scripts, if the first script runs correctly, and the second one and so on.

For example, let's say you want to run two commands, and if the first command was successful then run the second command. The correct syntax to do this is

command1.sh && command2.sh

This means the command1.sh will be executed and if no error occurs then command2.sh will be executed next.

Here's a real example, I created two bash scripts, command1.sh and command2.sh

command1.sh code

echo "hi from command1"

command2.sh code

echo "hi from command2"

each script will display it's own message since there was no error

Let's s try now to produce an error by adding exit 1 on the first script, making sure the first will exit before displaying the echo message

exit 1
echo "hi from command1"

and run both scripts again

And as expected, none of the scripts ran.  

Note: You can also use commands instead of the bash script, or both, commands and bash scripts, that's up to you. Here's another example using commands

If downloads folder exists then display message "Yes, it exists"

cd Downloads && echo "Yes, it exists"

In the example above we are displaying the message only if Downloads folder exists, and if not then nothing will be shown on the terminal

To show the error message when the downloads folder is not found then we can use the or operator | |

cd Downloads1 && echo "Yes, it exists" || echo "No, it doesn't exist"

Conclusion

In this tutorial, we explained step by step how to run a single or multiple bash scripts in background, using the nohup command and by using the logical operators & and && in bash.