What is the command you use in Linux to run the process in the background?
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. Show
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 (&) signTo 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 STDOUTor STDERRwhich will prevent you from executing other commands on the terminal. 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. 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 Running shell command or script in background using nohup commandAnother 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 ConclusionIn 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 Use the Bash shell in Linux to manage foreground and background processes. You can use Bash’s job control functions and signals to give you more flexibility in how you run commands. We show you how. All About ProcessesWhenever a program is executed in a Linux or Unix-like operating system, a process is started. “Process” is the name for the internal representation of the executing program in the computer’s memory. There is a process for every active program. In fact, there is a process for nearly everything that is running on your computer. That includes the components of your graphical desktop environment (GDE) such as GNOME or KDE, and system daemons that are launched at start-up. Why nearly everything that is running? Well, Bash built-ins such as cd, pwd,
and alias do not need to have a process launched (or “spawned”) when they are run. Bash executes these commands within the instance of the Bash shell that is running in your terminal window. These commands are fast precisely because they don’t need to have a process launched for them to execute. (You can type Processes can be running in the foreground, in which case they take over your terminal until they have completed, or they can be run in the background. Processes that run in the background don’t dominate the terminal window and you can continue to work in it. Or at least, they don’t dominate the terminal window if they don’t generate screen output. A Messy ExampleWe’ll start a simple ping www.howtogeek.com We get the expected results, scrolling down the terminal window. We can’t do anything else in the terminal window while Ctrl+C The visible effect of the Let’s repeat that. But this time we’ll hit ping www.howtogeek.com Ctrl+Z The visible effect of hitting This time we are told the process is stopped. Stopped doesn’t mean terminated. It’s like a car at a stop sign. We haven’t scrapped it and thrown it away. It’s still on the road, stationary, waiting to go. The process is now a background job. The jobs ps T The
The
These are common values for the STAT column:
The value in the STAT column can be followed by one of these extra indicators:
We can see that Bash has a state of The ping command has a state of The The bg CommandThe If we issue the bg The But we have a problem. The task is running in the
background and won’t accept input. So how do we stop it? In fact, we’re now in a strange blended mode. We can type in the terminal window but what we type is quickly swept away
by the scrolling output from the To stop our background task we need to bring it to the foreground and then stop it. The fg CommandThe If we type fg And now that we have the Ctrl+C We Need to Send the Right SignalsThat wasn’t exactly pretty. Evidently running a process in the background works best when the process doesn’t produce output and doesn’t require input. But, messy or not, our example did accomplish:
When you use Here are some of the commonly used ones.
We must use the Further Job ControlA process moved into the background by using Although it is best that background processes do not write to the terminal window, we’re going to use examples that do. We need to have something in the screenshots that we can refer to. This command will start an endless loop as a background process: while true; do echo “How-To Geek Loop Process”; sleep 3; done & We are told the job number and process ID id of the process. Our job number is 1, and the process id is 1979. We can use these identifiers to control the process. The output from our endless loop starts to appear in the terminal window. As before, we can use the command line but any commands we issue are interspersed with the output from the loop process. ls To stop our process we can use
jobs kill %1 RELATED: How Linux Signals Work: SIGINT, SIGTERM, and SIGKILL
kill 9 %1 Things We’ve Covered
RELATED: How to Kill Processes From the Linux Terminal READ NEXT
Which command will make process to run in background?If you know you want to run a command in the background, type an ampersand (&) after the command as shown in the following example. The number that follows is the process id. The command bigjob will now run in the background, and you can continue to type other commands.
Which command will make process to run in background in Linux sleep?bg. Jobs that are suspended in background can be started in background with bg. The bg will send a SIGCONT signal. Below an example of the sleep command (suspended with Ctrl-Z) being reactivated in background with bg.
|