About nixCraft

Topics

FreeBSD : Job management

Posted by Vivek Gite [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.

Tell us how we're doing: Please answer a few questions about your experience to help us improve nixCraft.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. Anonymous Says:

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

  2. Vivek Gite Says:

    > $ bg [ENTER]

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

    Appreciate your post.

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.