LINUX TIPS AND TRICKS --- October 05, 2001

Published by ITworld.com -- changing the way you view IT
http://www.itworld.com/newsletters
_________________________________________________________________

Job Control
By Danny Kalev

To distinguish them from system processes, the commands that 
users execute from a shell or a script file are often called 
"jobs". This week, I will show you how a set of job control 
operations can allow the user to control the execution of such 
jobs from the command shell.

Background Execution
Placing an ampersand at the end of the command executes a job in
the background. The system assigns each user's job a unique 
number in addition to a PID. When you execute a command in the 
background, a user's job number and a system process id appear 
on the screen. For example:

    $ lpr payroll &
    [1]   236
    $

The number in brackets is the user's job id; the second number 
is the system process's PID. You can place multiple commands in 
the background at once by separating each command with an 
ampersand. For example:

    $ lpr july & cat *.c &

The "jobs" command lists the current background jobs. Jobs 
displays each entries' job number in brackets, whether it's 
stopped or running, and its name:

    $ jobs
    [1]   +   Running   lpr july
    [2]   -   Running   cat *.c

A plus sign indicates that the job is currently being processed.
A minus sign indicates the next job to be executed.

Notification
By default, Linux doesn't interrupt other operations, say an 
editing session, to notify you that a certain job has 
completed. If you wish to be notified on a job's completion, 
then use the "notify" command. Notify takes a job number 
preceded by a percent sign as its argument:

    $ notify %2

This will cause the system to notify you when job 2 has 
completed, regardless of your current activity. 

Switching between Background and Foreground 
You can bring a background job to the foreground by executing 
the "fg" command. If there are several background jobs, then 
you must indicate which job you wish to move to the foreground 
by indicating its job number:

    $ fg %2

Likewise, you can move a currently running foreground job to 
the background with the "bg" command. In order to do that, you 
first need to suspend the job by pressing CTRL-Z. Then issue 
the bg command to resume the suspended job's execution in the 
background. For example:

    $ lpr july
    ^Z
    $ bg

Killing a Job
To cancel a running job, use the "kill" command. kill takes a 
PID or a job number preceded by % as its argument. In the 
following example, the user examines the currently running jobs 
and cancels job 2:

    $ jobs
    [1]   +   Running   lpr july
    [2]   -   Running   cat *.c
    $ kill %2
    $
_________________________________________________________________

About the author(s)
-------------------
Danny Kalev is a system analyst and software engineer with more 
than 10 years of experience, specializing in C++ and 
object-oriented analysis and design on various platforms 
including VMS, DOS, Windows, Unix, and Linux. His technical 
interests involve code optimization, networking, and distributed 
computing. He is also a member of the ANSI C++ standardization 
committee and the author of ANSI/ISO C++ Professional 
Programmer's Handbook (Que, 1999). Danny can be reached at 
linuxnl@excite.com.
_________________________________________________________________

ADDITIONAL RESOURCES

Configuring Job Controls
http://itw.itworld.com/GoNow/a14724a43301a76537031a0

PID Control Technical Notes
http://itw.itworld.com/GoNow/a14724a43301a76537031a2

What Is PID - Tutorial Overview
http://itw.itworld.com/GoNow/a14724a43301a76537031a3
_________________________________________________________________

ITWORLD.COM NEWSLETTER ARCHIVE

Index of Linux Tips and Tricks
http://www.itworld.com/nl/lnx_tip/

Writing to syslog 
http://www.itworld.com/nl/lnx_tip/11032000/

Introducing Firewalls
http://www.itworld.com/nl/lnx_tip/11172000/
_________________________________________________________________

CUSTOMER SERVICE

SUBSCRIBE/UNSUBSCRIBE:
- Go to: http://www.itworld.com/newsletters
- Click on "View my newsletters" to log in and manage your
  account
- To subscribe, check the box next to the newsletter
- To unsubscribe, uncheck the box next to the newsletter 
- When finished, click submit

Questions? Please e-mail customer service at: 
mailto:support@itworld.com
_________________________________________________________________

CONTACTS

* Editorial: Andrew Santosusso, Newsletter Editor, 
  andrew_santosusso@itworld.com

* Advertising: Clare O'Brien, Vice President of Sales, 
  clare_obrien@itworld.com

* Recruitment advertising: Jamie Swartz, Eastern, Regional Sales 
  Manager, jamie_swartz@itworld.com or Paul Duthie, Western
  Regional Sales Manager, paul_duthie@itcareers.net

* Other inquiries: Jodie Naze, Senior Product Marketing Manager, 
  jodie_naze@itworld.com
_________________________________________________________________

PRIVACY POLICY
http://www.itworld.com/Privacy/

Copyright 2001 ITworld.com, Inc., All Rights Reserved.
http://www.itworld.com
