You know all about multitasking on the desktop, but some Linux users aren't aware that you can do exactly the same kind of things in the console, albeit with a little more hands on control. In this article we'll run through the basics of job control in Linux.

In a graphical environment, such as your window manager, you can run multiple console tasks at once simply by opening more terminal emulators (such as xterm, aterm, etc). If you're stuck with just the single terminal, or you just don't want to clutter your desktop with a lot of windows, then you need to control the running of console programs.

Foreground vs Background

When you start a program normally in the console it runs in the foreground. Foreground programs have access to the standard input stream, allowing them to receive commands from the user, and the standard output stream, allowing them to print information back to the user.

Programs running in the background do not have access to these streams, which means they cannot interact with the user, but are unchanged otherwise. If your task doesn't require that kind of input and output, then you can run it in the background and continue to use the console as normal.

Examples of jobs that are often run in the background are long operations that do not require input, such as compiling or building software, or programs that interact with the user through a GUI and not the console.

You can start any program in the background by adding a "&" character to the end of the line. For example if we were starting Firefox in the console we'd write:

$ firefox &
[1] 936
$

Firefox starts and control of the terminal is returned immediately. You can see that two numbers are printed in the console. The first, between the brackets, is the job number, which can be used to control the job. The second is the Pid (process identifier), which is a unique, system wide identifier for the process.

You can also suspend an already running process by hitting CTRL-Z during it's operation. For example, if we were to load vim, and then hit CTRL-Z we'd see the following:

$ vim

[1]+  Stopped                 vim
$

Again the job number is printed, as well as the program name. Suspending a program does not make it run in the background, rather it stops the process in place, ready to be resumed at the users command.

Restarting suspended processes

You can restart a process either in the foreground or in the background.

To restart a process in the foreground, use the command fg followed by the job number.

$ vim

[1]+  Stopped                 vim
$ fg 1
vim

This example will load vim, then after we hit CTRL-Z will suspend it, the fg command starts it running again in the foreground, bringing up the vim console window.

Likewise, you can restart a process in the background using the bg command. In the following example we start an xterm in the foreground. This blocks our console as it has claimed the input and output. When we hit CTRL-Z the process is suspended, which gives us the console back but does not allow us to type into the new terminal. By typing bg 1, we run the xterm in the background -- making both terminals available:

$ xterm

[1]+  Stopped                 xterm
$ bg 1
[1]+ xterm &
$ 
[1]+  Done                    xterm

When we close the xterm window the process exits, and the Done message is printed in our original prompt to let us know. Be careful, as this can interfere with the output of other programs you run in the original terminal.

Examining and removing current jobs

To take a look at what jobs are currently running, run the jobs program:

$ jobs
[1]+  Stopped                 vim
[2]-  Running                 xterm &

This shows us that there are two programs running, job 1 is vim, which is suspended. Job 2 is xterm which is running in the background. With this information we can move either into the foreground or background with fg and bg.

To kill a job we use the appropriately titled program kill. Usually kill is used with a process identifier -- the same as was printed when we started the program with "&". The process identifiers of all running processes can be found by using the ps command:

$ ps
  PID TTY          TIME CMD
 1260 pts/0    00:00:00 bash
 1537 pts/0    00:00:00 vim
 1672 pts/0    00:00:00 xterm
 1690 pts/0    00:00:00 ps

This shows the four programs we have running in this console: the shell (in this case bash), our two programs, vim and xterm and the ps command we just ran. We can kill any of these processes by typing kill followed by the process identifier:

$ kill 1672
$

And the xterm window disappears.

Finding and typing out the process identifier is often more of a hassle than you want at any given moment, but if you know the job number there is a quicker way. You can replace the process identifier in the previous example with the job number preceded by a "%" character:

$ kill %1

[1]+  Stopped                 vim

The program may not be killed immediately if it is suspended, but should exit as soon as it becomes active -- either in the foreground or background. You can avoid this by telling the kill program to force the program to end using the -9 flag:

$ kill -9 %1
$

Be careful with this flag however, forcing too many programs to close can leave your system unstable, since they are not given a chance to clean up after themselves.

While the ubiquity of graphical desktops have made job control not quite so vital a skill for UNIX and Linux users, learning how to manage jobs in your terminal can make your workspace neater and make you more productive. Are there more tips in this area that I've left out? Share your knowledge with the community by posting below.

Do you need help with Linux? Gain advice from Builder AU forums

Related links

Comments

1

CLI-nubee - 30/08/07

If you have just a single job suspended (or hit ctrl-z by mistake), 'fg' will bring it back. No need for the job number. The 'top' command (I like 'htop') seems to deserve a mention here as a related tool. Cheers!

» Report offensive content

2

adam - 02/09/07

"Finding and typing out the process identifier is often more of a hassle..." <- there is a command group called pgrep/pkill each greping or killing the task by name. It should only be used with caution. (available on solaris, linux, freebsd, netbsd, etc.)

» Report offensive content

3

GaZnooB - 20/08/08

Thanks, very interesting. just what i needed :-)

» Report offensive content

4

Vladimir - 02/02/09

$ jobs
[1]+ Stopped vim
[2]- Running xterm &

What does + and - signs stand for?

Thanks

» Report offensive content

5

Angus - 24/02/09

It would seem from the man pages for jobs that the plus sign indicates that this job would be the default job to put in the foreground if fg was called, and the minus sign indicates that the job would be graduated to "plus" state if the current job in "plus" state were to exit.

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

5

Angus - 24/02/09

It would seem from the man pages for jobs that the plus sign indicates that this job would be the default ... more

4

Vladimir - 02/02/09

$ jobs [1]+ Stopped vim [2]- ... more

3

GaZnooB - 20/08/08

Thanks, very interesting. just what i needed :-) ... more

Log in


Sign up | Forgot your password?

  • Staff Aussies to pay more for Win 7

    If you are looking to make some money in these troubled times, perhaps importing copies of Windows 7 could be for you. Read more »

    -- posted by Staff

  • Staff Firefox: Greens want it, 3.5rc2 not up to par

    This week's roundup looks at the situation surrounding a campaign to change Outlook HTML renderer, a Greens MP wants to install Firefox but is restricted and all the photos from the iPhone 3GS launch. Read more »

    -- posted by Staff

  • Chris Duckett Microsoft misses the Outlook point

    Ask designers which mail program is the bane of their existence, and you'll find that Outlook tops the list. The reason why the most popular email reader is also the most painful is simple: it uses Word to render HTML emails. Read more »

    -- posted by Chris Duckett

What's on?