The usual style of executing a command on a Linux terminal is to simply run it and wait for it to gracefully exit. Once the command exits, you can then proceed to execute other commands in succession. This is what is known as running commands in the foreground. As the word suggests, you can visually see the output of the command on the terminal.

Sometimes, however, running commands in the foreground can present a set of challenges. The command can take too long to exit causing you to waste precious time and other times, it can be totally attached to the shell session leaving you stuck.

In such cases, running a command in the background is your best bet. You can send a command(s) to the background as you concurrently execute other commands in the foreground. This improves the efficiency of working on the terminal and saves you time.

In this guide, we focus on how you can run Linux shell command or script in the background.

Running shell command in background using (&) sign 

To run a command or a script to the background, terminate it with an ampersand sign (&) at the end as shown.

$ command &

NOTE: Ending the command with the ampersand sign does not detach the command from you. It merely sends it to the background of the current shell that you are using, the command will still print the output to STDOUT or STDERR which will prevent you from executing other commands on the terminal.

linux-ping-command-background

A better approach is to redirect the command to /dev/null and later append the ampersand sign at the end as shown

$ command &>/dev/null &

To confirm that the command was sent to the background, run the jobs command.

linux-shell-command-dev-null

To terminate the background process, run the kill command followed by the PID of the process as follows. The -9 option terminates the process immediately.

$ kill -9 138314

kill-linux-background-command

Running shell command or script in background using nohup command

Another way you can run a command in the background is using the nohup command. The nohup command, short for no hang up, is a command that keeps a process running even after exiting the shell.

It does this by blocking the processes from receiving a SIGHUP (Signal Hang UP) signal which is a signal that is typically sent to a process when it exits the terminal.

To send a command or script in the background and keep it running, use the syntax:

$ nohup command &>/dev/null &

$ nohup shell-script.sh &>/dev/null &

For example,

$ nohup ping google.com &>/dev/null &

Again, you can verify that the command is running in the background using the command:

$ jobs

nohup-linux-shell-command

Conclusion

In this guide, we have covered two ways of running commands or shell scripts in the background. We have seen how you can run commands in the background using the & sign and the nohup command.

Read Also : Linux Zip and Unzip Command with Examples

The post How to Run Linux Shell Command / Script in Background first appeared on LinuxTechi.

Leave a Comment