Linux: Working with jobs

I’ve noticed that many people I work with and have to do something in a Linux shell tend to not use jobs much. I’m not too sure whether it’s because they do not know how to use them or because it isn’t something they think about automatically. I actually use them very much. It might be because I’m getting old and keep forgetting to write an ampersand at the end of the line or because I don’t like have 10 terminal windows open…

Jobs are basically just commands you’ve started and run in the background or in the foreground. Foreground and background jobs are pretty similar, the difference is that for a foreground job, what you type in the terminal is directed to the standard input of the job (stdin) and for a background job it is goes to the shell process instead.

I guess everybody has already used jobs by starting something in the background using the ampersand e.g.:

# find / -name myfilename > /tmp/search.results

Would execute in the foreground i.e. block the terminal until it finishes and the following would run it as a background job:

# find / -name myfilename > /tmp/search.results &
[4] 17974

[4] means it’s the job number 4. This will be useful to know in order to bring it back to the foreground and send it again in the background.

If it finishes while still in the background, it will write the following to the console:

[4]   Exit 1                  find / -name myfilename > /tmp/search.results

Now, if you forgot the ampersand and started it in the foreground, you can suspend it by pressing Ctrl-Z:

# find / -name myfilename > /tmp/search.results
^Z
[1]+  Stopped                 find / -name myfilename > /tmp/search.results

The job is now stopped i.e. suspended and waiting to be resumed in the background or in the foreground. The plus after the job number means that it is the current process.

If you now call the command fg, it will bring it back to the foreground (it brings the current job back to the foreground):

# fg
find / -name myfilename > /tmp/search.results

And if you call the command bg instead, it will bring it back to the background:

# bg
[1]+ find / -name myfilename > /tmp/search.results

So if you started some long running job in the foreground and need to do something else in your shell but do not want to or cannot open another terminal window, just press CTRL-Z to suspend it, use bg to resume it in the background, do whatever you need to and then use fg to bring it back to the foreground.

Now if you do not want to resume the current job to the background or the foreground but another job you’ve suspended earlier, you can use the job number to resume a specific job:

# bg %2
[2]- find / -name 2myfilename > /tmp/search.results &
# bg %3
[3]+ find / -name 2myfilename > /tmp/search.results &
# fg %1
find / -name 2myfilename > /tmp/search.results

You can see all jobs using the jobs command:

# jobs
[1]   Stopped                 find / -name *myfilename* > /tmp/search.results
[2]-  Stopped                 find / -name *myfilename2* > /tmp/search.results
[3]+  Stopped                 find / -name *myfilename3* > /tmp/search.results

If you have some background jobs, it will look like this:

# jobs
[2]-  Stopped                 find / -name *myfilename2* > /tmp/search.results
[3]+  Stopped                 find / -name *myfilename3* > /tmp/search.results
[4]   Running                 find / -name *myfilename4* > /tmp/search.results &

You can also use the kill command to kill these jobs:

# kill %3
[3]   Terminated              find / -name *myfilename3* > /tmp/search.results

Of course, you all know that you can kill the current job by pressing CTRL-C.

One thought on “Linux: Working with jobs

Leave a Reply

Your email address will not be published.