How many times did you start something which is time consuming and waited for it to end until you could use the console again. We have all faced a situtation when we just stare at the screen for a process to get over after we have initiated it. It could be something as simple as copying a large file from a removable media or installing packages using yum/apt-get from online repositories over a slow internet connection (ouch!). Once started you will not get back the command shell until the process is done, so what do you do ? Hit Ctrl+C terminate it and hope to do it over the weekend ? Take a coffee break ? Count the number of keys on the keyboard ? Why not press Ctrl+Z and get the work done in background while you use the console to do something else. In this article I will cover how to suspend a process a.k.a. job, run it in the background, bring it to the foreground, terminate it if necessary.
Viewing the status of jobs
The “jobs” command will display background jobs and their current status.
root@debian:~# jobs
[1]+ Stopped apt-get upgrade
[2]- Running dd if=/dev/zero of=dummyfile bs=1K count=2048000 &
Executing a job in the background
If you are well aware that something will take a long time to finish why not start it in the background itself. Just adding an ampersand (&) after the command will execute it in background.
cp /mnt/largefile /home/jesin/files/largefile.bak &
Moving a foreground job to the background
You have started something thinking it will end soon but it is taking an eternity to complete, just a simple Ctrl+Z is enough to move it back
root@debian:~# dd if=/dev/zero of=dummyfile bs=1K count=2048000
^Z
[1]+ Stopped dd if=/dev/zero of=dummyfile bs=1K count=2048000
That ^Z denotes hitting Ctrl+Z. Now that the job is stopped use the bg command to run it in the background.
root@debian:~# bg %1
The number “1” is the ID of the job as viewed under the jobs command it must be preceded with a “%”
Moving a background job to the foreground
To bring a job running in the background to the foreground use the fg command
root@debian:~# fg %1
Terminating a job
Terminating or killing a job can be done using the “kill” command. But wait a minute isn’t this command used to terminate processes ? Yes it is so it all depends on the argument passed. The job number should be entered preceded by a percentage symbol (%)
root@debian:~# kill %2
Be very careful not to miss the percentage symbol, miss it and you will end up terminating PID 1 which is “init” or PID 2 which is “kthreadd” terminating any of these will make your life miserable.
Suspend a background job
Now this is a bit tricky as there is no direct command to make this happen, you need to use a couple of commands to stop a process running in the background. The process we follow is this – note the jobID, bring the job to the foreground, press CTRL+Z to suspend it.
root@debian:~# jobs
[1]+ Running dd if=/dev/zero of=dummyfile bs=1K count=2048000 &
root@debian:~# fg %1
^Z
[1]+ Stopped dd if=/dev/zero of=dummyfile bs=1K count=2048000
Arthur says
Don’t forget
disown - h %1
with bash. This let’s you close the shell and have the process still run.