Linux/Unix – Handling and managing jobs.

Managing Jobs

 The processes you have running in a particular shell are called your jobs. You can have more than one job running from a shell at once, but only one job can be active at the terminal, reading standard input and writing standard output. This job is the foreground job, while any other jobs are said to be running in the background.

The shell assigns each job a unique job number. Use the job number as an argument to specify the job to commands. Do this by giving the job number preceded by a `%’ character.

 

Suspending a Job

Type C−z to suspend or stop the foreground job−−useful for when you want to do something else in the shell and return to the current job later. The job stops until you either bring it back to the foreground or make it run in the background.

For example, if you are reading a document in info, typing C−z will suspend the info program and return you to a shell prompt where you can do something else. The shell outputs a line giving the job number (in brackets) of the suspended job, the text `Stopped’ to indicate that the job has stopped, and the command line itself, as shown here:

[1]+ Stopped info −f cookbook.info

In this example, the job number is 1 and the command that has stopped is `info −f cookbook.info’. The `+’ character next to the job number indicates that this is the most recent job. If you have any stopped jobs when you log out, the shell will tell you this instead of logging you out:

$ logout

There are stopped jobs.

$

At this point you can list your jobs, stop any jobs you have running and then log out.

 

Putting a Job in the Background

New jobs run in the foreground unless you specify otherwise. To run a job in the background, end the input line with an ampersand (`&’). This is useful for running non−interactive programs that perform a lot of calculations. To run the command apropos shell > shell−commands as a background job, type:

$ apropos shell 62; shell−commands 38;

[1] 6575

$

The shell outputs the job number (in this case, 1) and process ID (in this case, 6575), and then returns to a shell prompt. When the background job finishes, the shell will list the job number, the command, and the text `Done’, indicating that the job has completed successfully:

[1]+ Done apropos shell 62;shell−commands

To move a job from the foreground to the background, first suspend it (see section Suspending a Job) and then type bg (for “background”). For example, to start the command apropos shell > shell−commands in the foreground, suspend it, and then specify that it finish in the background, you would type:

$ apropos shell 62; shell−commands

C−z

[1]+ Stopped apropos shell 62;shell−commands

$ bg RET

[1]+ apropos shell 38;

$

If you have suspended multiple jobs, specify the job to be put in the background by giving its job number as an argument. To run job 4 in the background, type:

$ bg %4

NOTE: Running a job in the background is sometimes called “backgrounding” or “amping off” a job.

 

Putting a Job in the Foreground.

Type fg to move a background job to the foreground. By default, fg works on the most recent background job. To bring the most recent background job to the foreground, type:

$ fg

To move a specific job to the foreground when you have multiple jobs in the background, specify the job number as an option to fg. To bring job 3 to the foreground, type:

$ fg %3

 

Listing Your Jobs

To list the jobs running in the current shell, type jobs. To list your jobs, type:

$ jobs

[1]− Stopped apropos shell 62;shell−commands

[2]+ Stopped apropos bash 62;bash−commands

$

This example shows two jobs−−−apropos shell > shell−commands and apropos bash > bash−commands. The `+’ character next to a job number indicates that it’s the most recent job, and the

`−’ character indicates that it’s the job previous to the most recent job. If you have no current jobs, jobs return nothing. To list all of the processes you have running on the system, use ps instead of jobs−−−see Listing System Activity.

 

Stopping a Job

Typing C−c interrupts the foreground job before it completes, exiting the program. To interrupt cat, a job running in the foreground, type:

$ cat

C−c

$

Use kill to interrupt (“kill”) a background job, specifying the job number as an argument. · To kill job number 2, type:

$ kill %2

 

Killing all the process for some user

For kill all the processes for a specific user in any system you can use the following command:

kill -9 `ps -U <idsid>| awk ‘{ print $1 }’`

For example:

1.- Login into the machine when you need to kill all the process for an user.

2.- write “ps –aux | grep <idsid> in order to be sure regarding the process of the user.

3.- Apply the command: kill -9 `ps -U mgsotoso | awk ‘{ print $1 }’`

 

Advertisements

About msotela

This blog is for anyone who wants to access the power of a Linux system as a systems administrator or user. You may be a Linux enthusiast, a Linux professional, or possibly a computer professional who is increasingly finding the Windows systems in your data center supplanted by Linux boxes.

Posted on March 16, 2009, in Unix/Linux. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: