FreeBSD : Job management

by on June 19, 2005 · 4 comments· LAST UPDATED June 19, 2005

in

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.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 4 comments… read them below or add one }

1 Anonymous June 19, 2005 at 9:45 pm

>OR
>
>$ 1% &
>[1]+ gpdf &
OR simply
$ bg [ENTER]

Reply

2 Vivek Gite June 20, 2005 at 12:23 am

> $ bg [ENTER]

Yup, you can do that! Thanks for pointing out.

Appreciate your post.

Reply

3 chuck August 9, 2012 at 3:06 pm

“i)Start command in background without appending &
$ xmms”

dont’ you mean

i)Start command in background without suspending
$ xmms

Reply

4 gthf October 30, 2012 at 1:38 am

$ 1% &
[1]+ gpdf &

should be:

$ %1 &
[1]+ gpdf &

Reply

Leave a Comment

Previous post:

Next post: