FreeBSD : Job management

Posted on in Categories News last updated June 19, 2005

Most of the new comers in UNIX world aware of ‘How to start a application, job, or even background process from command line’. However shell offers extensive job control like
putting a running job in the background, suspend a job, list it, restart it and so on!

A) To start a job in background use following syntax:
command-name &

You would like to start xmms to listen mp3s, you start it using following command:
xmms &

In reality many time you forget to append & at the end and your command; most of the us hit CTRL+C to cancel the xmms and restart it with appending & to it. However, you can take advantage of job management as follows:

i)Start command in background without appending &
$ xmms

ii)Press CTRL+Z to suspend it (you will get job number on screen)
[1]+ Stopped gpdf

iii) Type % &
OR
job-number & (remember % refer to last suspended job so no need to type job number)

$ % &

OR

$ 1% &
[1]+ gpdf &

This will save lot of time for example if you are running a job at shell prompt, and it is taking lots of time. Then use above trick. However, classic book on UNIX suggest following steps:

a) Start a job
$ xmms

b) Hit CTRL+Z to suspend it

c) Lists the active jobs with jobs command:
$ jobs
[1]- Running gpdf &
[2]+ Stopped vi

d) Place a job (with given number) in the background
$ bg %2

e) If you wish you can also put a job in foreground with command:
$ fg %2

f) You can list only job numbers with
$ jobs -p

g) Naturally kill all your jobs using kill command:
$ kill -9 $(jobs -p)
OR
$ kill -9 `jobs -p`

Please note that job management is shell feature and almost available on all type of UNIX/BSD/Linux operating systems. Therefore, commands discussed here should work on Linux, Solaris, HP-UX et all.

1) Read man page of bash
2) Read help pages for bg, fg,and jobs commands, for example:
$ help jobs
3) See screen shot of above commands.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

4 comment

Leave a Comment