When an application runs, it is possible that it will run for a long period of time or run until the computer shuts down. While running an application in a shell, we know that a shell prompt only comes back when running a program in the shell completes successfully or terminates due to some error. Unless we get a shell prompt back, we can't run another command in the same shell. We can't even close that shell because it will close the running process.
Also, to run another application, we will have to open another shell in a new terminal and then run it. It can become difficult and tedious to manage if we have to run a lot of tasks. Shells provide ways to run a task in the background and suspend, kill, or move back in the foreground.
A task can be started as a background in a shell by appending an ampersand (&).
For example, we want to search for a string in the entire filesystem. Depending upon the filesystem's size and the number of files, it may take a lot of time. We can call the grep
command to search for a string and save the result in a file. A filesystem hierarchy in Linux starts from the root('/
').
$ grep -R "search Text" / 2>/dev/null > out1.txt & [1] 8871 $
Here, the grep
searches for a string in the entire filesystem, sends any error message to /dev/null
, and saves the search result into the out1.txt
file. An ampersand (&) at the end sends the entire job to the background, prints PID of the started task, and returns back the shell prompt.
Now, we can do other work in the same opened shell and perform other tasks.
It often happens that we run a task in a shell normally—that is, as a foreground task—but later we want to move it to the background. It is possible to do this by first suspending the current task using [Ctrl + z] and then using bg
to move the task to the background.
Consider the last text search as an example. We start a search normally as follows:
$ grep -R "search Text" / 2>/dev/null > out2.txt
We will not see anything happening on the shell and we will just keep waiting for a shell prompt to return. Alternatively, we can suspend the running job using [Ctrl + z]:
[ctrl + z] [2]+ Stopped grep -R "search Text" / 2> /dev/null > out2.txt
Then, to send a suspended task to continue running in the background, use the bg
command:
$ bg [2]+ grep -R "search Text" / 2> /dev/null > out2.txt
To find out which tasks are running in the background or suspended in the current shell, jobs
shell built - in is used as follows:
$ jobs
[1]- Running grep -R "search Text" / 2> /dev/null > out1.txt & [2]+ Running grep -R "search Text" / 2> /dev/null > out2.txt &
Here, index [1] and [2] are job numbers.
The character '+
' identifies the job that would be used as a default by the fg
or bg
command, and the character '-
' identifies the job that would become a default if the current default job exits or terminates.
Create another task and suspend it using the following commands:
$ grep -R "search Text" / 2>/dev/null > out3.txt
[ctrl + z]
[3]+ Stopped grep -R "search Text" / 2> /dev/null > out3.txt
$ jobs
[1] Running grep -R "search Text" / 2> /dev/null > out1.txt &
[2]- Running grep -R "search Text" / 2> /dev/null > out2.txt &
[3]+ Stopped grep-R "search Text" / 2> /dev/null > out3.txt
To view PID of all background and suspended tasks, we can use the –p
option:
$ jobs -p
8871 8873 8874
PID of jobs is in sequence. To view only the tasks running in the background, the -r
option is used as follows:
$ jobs -r
[1] Running grep -R "search Text" / 2> /dev/null > out1.txt & [2]- Running grep -R "search Text" / 2> /dev/null > out2.txt &
To view only the suspended tasks, the -s
option is used as follows:
$ jobs -s
[3]+ Stopped grep-R "search Text" / 2> /dev/null > out3.txt
To view a particular index job, use an index number with the jobs
command:
$ jobs 2
[2]- Running grep -R "search Text" / 2> /dev/null > out2.txt &
We can move a background or suspended task to the foreground using the shell built - in command fg
:
$ jobs # Listing background and suspended tasks
[1] Running grep -R "search Text" / 2> /dev/null > out1.txt & [2]- Running grep -R "search Text" / 2> /dev/null > out2.txt & [3]+ Stopped grep-R "search Text" / 2> /dev/null > out3.txt
The character '+
' is mentioned in the job index 3
. This means, running the fg
command will run the third job in the foreground:
$ fg $ grep -R "search Text" / 2> /dev/null > out3.txt [ctrl + z] [3]+ Stopped grep -R "search Text" / 2> /dev/null > out3.txt
The following command suspends the third task:
$ jobs [1] Running grep -R "search Text" / 2> /dev/null > out1.txt & [2]- Running grep -R "search Text" / 2> /dev/null > out2.txt & [3]+ Stopped grep-R "search Text" / 2> /dev/null > out3.txt
To move a particular job to the foreground, use fg
with a task index number:
$ fg 1 # Moving first tasks to foreground $ grep -R "search Text" / 2> /dev/null > out1.txt [ctrl + z] [1]+ Stopped grep -R "search Text" / 2> /dev/null > out1.txt
We can also delete a running or suspended task if it's no longer needed. This can be done by using the disown
shell built - in command:
$ jobs # List running or suspended tasks in current shell
[1]+ Stopped grep -R "search Text" / 2> /dev/null > out1.txt [2] Running grep -R "search Text" / 2> /dev/null > out2.txt & [3]- Stopped grep -R "search Text" / 2> /dev/null > out3.txt
Using disown
without any option, deletes a task that has the character '+
' mentioned with a task:
$ disown
bash: warning: deleting stopped job 1 with process group 8871
$ jobs # Listing available jobs
[2]- Running grep -R "search Text" / 2> /dev/null > out2.txt &
[3]+ Stopped grep -R "search Text" / 2> /dev/null > out3.txt
To delete running tasks, the -r
option is used:
$ disown -r
jobs
[3]- Stopped grep -R "search Text" / 2> /dev/null > out3.txt
To remove all tasks, the -a
option is used as follows:
$ disown -a # Gives warning for deleting a suspended task bash: warning: deleting stopped job 3 with process group 8874 $ jobs
The output of jobs
shows nothing because all the suspended and running tasks got deleted by the -a
option.
3.144.26.221