Welcome to Linux Knowledge Base and Tutorial
"The place where you learn linux"
Save the Children

 Create an AccountHome | Submit News | Your Account  

Tutorial Menu
Linux Tutorial Home
Table of Contents
Up to --> Linux Tutorial

· Shells and Utilities
· The Shell
· The Search Path
· Directory Paths
· Shell Variables
· Permissions
· Regular Expressions and Metacharacters
· Quotes
· Pipes and Redirection
· Interpreting the Command
· Different Kinds of Shells
· Command Line Editing
· Functions
· Job Control
· Aliases
· A Few More Constructs
· The C-Shell
· Commonly Used Utilities
· Looking for Files
· Looking Through Files
· Basic Shell Scripting
· Managing Scripts
· Shell Odds and Ends

Man Pages
Linux Topics
Test Your Knowledge

Site Menu
Site Map
Copyright Info
Terms of Use
Privacy Info
Masthead / Impressum
Your Account

Private Messages

News Archive
Submit News
User Articles
Web Links


The Web

Who's Online
There are currently, 68 guest(s) and 0 member(s) that are online.

You are an Anonymous user. You can register for free by clicking here

Linux Tutorial - Shells and Utilities - Job Control
  Functions ---- Aliases  

Job Control

Job control is the ability to move processes between the foreground and background. This is very useful when you need to do several things at once, but only have one terminal. For example, let's say there are several files spread out across the system that we want to edit. Because we don't know where they are, we can't use full paths. Because they don't have anything common in their names, we can't use find. So we try ls -R > more.

After a minute or two, we find the first file we want to edit. We can then suspend this job by pressing Ctrl+Z. We then see something that looks like this:

[1]+ Stopped ls -R | more

This means that the process has been stopped or suspended. One very important thing to note is that this process is not in the background as if we had put an "&" at the end. When a process is suspended, it stops doing anything, unlike a process in the background, which keeps on working.

Once the ls is in the background, we can run vi. When we are done with vi, we can bring the ls command back with the fg (foreground) command.

If we wanted to, we could have more than just one job suspended. I have never had the need to have more than two running like this, but I have gotten more than ten during tests. One thing that this showed me was the meaning of the plus sign (+). This is the "current" job, or the one we suspended last.

The number in brackets is the process entry in the job table, which is simply a table containing all of your jobs. Therefore, if we already had three jobs, the next time we suspended a job, the entry would look like this:

[4]+ Stopped ls -R >> output

To look at the entire job table, we simply enter the command jobs, which might give us

[1] Stopped ls -R /usr >> output.usr
[2] Stopped find / -print > output.find
[3]- Stopped ls -R /var >> output.var
[4]+ Stopped ls -R >> output.root

The plus sign indicates the job that we suspended last. So this is the one that gets called if we run fg without a job number. In this case, it was Job 4. Note that there is a minus sign (-) right after Job 3. This was the second to last job that we suspended. Now, we bring Job 2 in the foreground with fg 2 and then immediately suspend it again with Ctrl+Z. The table now looks like this:

[1] Stopped ls -R /usr >> output
[2]+ Stopped find / -print > output.find
[3] Stopped ls -R /var >> output
[4]- Stopped ls -R >> output

Note that Job 2 now has the plus sign following it and Job 4 has the minus sign.

In each of these cases, we suspended a job that was running in the foreground. If we had started a job and put it in the background from the command line, the table might have an entry that looked like this:

[3] Running ls -R /var >> output &

This shows us that although we cannot see the process (because it is in the background), it is still running. We could call it to the foreground if we wanted by running fg 3. And, if we wanted, we could use the bg command to send one of the stopped jobs to the background. So

would send Job 1 to the background just as if we had included & from the command line.

One nice thing is that we don't have to use just the job numbers when we are pulling something into the foreground. Because we know that we started a process with the find command, we can get it by using

fg %find

Actually, we could have used %f or anything else that was not ambiguous. In this case, we were looking for a process that started with the string we input. We could even look for strings anywhere within the command. To do this, the command might be

which would have given us the same command. Or, if we had tried

we would have gotten Job 1 because it contains the string usr.

If we find that there is a job that we want to kill (stop completely), we can use the kill command. This works the same way, so kill %<nr> kills the job with number <nr>, kill %<string> kills the job starting with string, and so on.

Keep in mind that process takes up resources whether they are in the foreground or not. That is, background processes take up resources,too.

If you do not remember the process ID of the last process that was placed in the background you can reference it any time using the $! system variable. You can also use the wait command to stop processing until the particular process is done. The syntax is simply:

wait PID

Although generally considered part of "job control" you can change the default priority a process has when it starts, as well as the process of a running process. Details of this can be found in the section on process scheduling.

 Previous Page
  Back to Top
Table of Contents
Next Page 


Test Your Knowledge

User Comments:

You can only add comments if you are logged in.

Copyright 2002-2009 by James Mohr. Licensed under modified GNU Free Documentation License (Portions of this material originally published by Prentice Hall, Pearson Education, Inc). See here for details. All rights reserved.



Security Code
Security Code
Type Security Code

Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.

Help if you can!

Amazon Wish List

Did You Know?
You can choose larger fonts by selecting a different themes.


Tell a Friend About Us

Bookmark and Share

Web site powered by PHP-Nuke

Is this information useful? At the very least you can help by spreading the word to your favorite newsgroups, mailing lists and forums.
All logos and trademarks in this site are property of their respective owner. The comments are property of their posters. Articles are the property of their respective owners. Unless otherwise stated in the body of the article, article content (C) 1994-2013 by James Mohr. All rights reserved. The stylized page/paper, as well as the terms "The Linux Tutorial", "The Linux Server Tutorial", "The Linux Knowledge Base and Tutorial" and "The place where you learn Linux" are service marks of James Mohr. All rights reserved.
The Linux Knowledge Base and Tutorial may contain links to sites on the Internet, which are owned and operated by third parties. The Linux Tutorial is not responsible for the content of any such third-party site. By viewing/utilizing this web site, you have agreed to our disclaimer, terms of use and privacy policy. Use of automated download software ("harvesters") such as wget, httrack, etc. causes the site to quickly exceed its bandwidth limitation and are therefore expressly prohibited. For more details on this, take a look here

PHP-Nuke Copyright © 2004 by Francisco Burzi. This is free software, and you may redistribute it under the GPL. PHP-Nuke comes with absolutely no warranty, for details, see the license.
Page Generation: 0.08 Seconds