How to Run a Shell Script in Background?

Linux and all of its distributions have two types of processes i.e “Foreground” and “Background”. The foreground process requires user interaction to start, while the “background” process runs without the user interaction with the terminal.

The “foreground” process takes a long time time to exit the shell because it can not execute the second process until the first is terminated. In such types of situations, the “background” process is best that keeps running in the background without concern with the terminal operations and processes.

Keeping this in view, this post lists down the possible ways to run a shell script in the background:

Method 1: Using the “&(ampersand)” Symbol

The “&(ampersand)” symbol is the control operator of a bash shell. It exits the shell without finishing the command execution written in the shell script. When the command is executed successfully, then it displays a message in the terminal i.e “Done”.

Syntax:

The “&(ampersand)” is used at the end of the command/script that the user wants to run in the background:

$ command &

Example:

Let’s move on to its practical implementation of “&” by following the below example.

Create Script

Create/open a bash script in the default “nano” text editor that contains the following lines of code:

$ nano script.sh
#!/bin/bash

sleep 20

echo "Welcome to its Linuxfoss"

In the above “script.sh”:

  • The “#!/bin/bash” line is a “Bash Shebang” that executes the script in a bash shell.
  • The “sleep” command is used to pause the script for “20” seconds.
  • The “echo” command will print the encoded statement after 20 seconds.

Save (Ctrl+S) and exit(Ctrl+X) the script.

Make the Script Executable

Assign the executable permissions to the newly created script with the help of the “chmod(change mode)” command followed by the “x(execute)” flag:

$ chmod +x script.sh

The “script.sh” is now executable.

Run Script in Background

Execute the script in the background by just specifying the “script.sh” with the “&” symbol in this way:

$ ./script.sh &

The “script.sh” is running in the background having the job id [1] and the process id “5633

View Background Script

The “jobs” command is used to list down the commands or scripts that are running in the background. 

Run the “jobs” command without any argument to perform this task:

$ jobs

The “script.sh” having job id “[1]” is in a running state in the background.

Output

After 20 seconds the output of the “script.sh” is displayed on the terminal automatically like this:

$ ./script.sh &

The background “script.sh” performed its job successfully.

Method 2: Using the “nohup” Command

The “nohup” stands for the “no hang up” command line tool that keeps running the long-run processes in the background. It does not kill the background process even if the current session is disconnected/logout. 

This operation is possible because it weakens the SIGHUP (Signal Hang UP) that is sent to a process when the terminal exits.

Syntax:

The generalized syntax to use the “nohup” command is written below:

$ nohup command &

The “&(ampersand)” is used with the “nohup” command to see the background process job id and the taste i.e stopped, running, or done.

Example:

The following example illustrates the “nohup” command working on running a shell script in the background.

Open a Script

The “home” directory contains the “sample.sh” script opened in the “nano” having the following commands:

$ nano script.sh

The “sample.sh” script will execute the first command instantly and then after 60 seconds the second echo statement will be executed.

Execute Script in Background

The “sample.sh” script is navigated to the background with a job id “[1]” and the process id “6119” via the “nohup” command:

$ nohup ./sample.sh &

The last line says there is no need for input to execute the shell script and the “output” of it is redirected to the “nohup.out” file.

Check Background Jobs

Run the jobs command to verify that the “sample.sh” script is running in the background:

$ jobs

Output

To check the “script.sh” output from the “nohup.out” file use the “tail” command that shows the last lines of the file followed by “-f(file)” type:

$ tail -f nohup.out

The “sample.sh” script is executed successfully.

Conclusion

To run a shell script in the background, use the “&(ampersand)” bash control operator and the built-in “nohup” command line tool. Both these utilities almost perform the same job. The core difference between both of them is that the “nohup” command does not kill the background process at the session termination while the “&” symbol terminates all processes after the system logout.

This post has provided all possible aspects to run a shell script in the background in Linux.