next up previous contents index
Next: A Few bash Features Up: The Basics Previous: The Shell   Contents   Index


Managing Processes with bash

Debian is a multitasking system, so you need a way to do more than one thing at once. Graphical environments like X provide a natural way to do this; they allow multiple windows on the screen at any one time. Naturally, bash (or any other shell) provides similar facilities.

Earlier you used top to look at the different processes on the system. Your shell provides some convenient ways to keep track of only those processes you've started from the command line. Each command line starts a job (also called a process group) to be carried out by the shell. A job can consist of a single process or a set of processes in a pipeline (more on pipelines later).

Entering a command line will start a job. Try typing man cp, and the cp manual page will appear on the screen. The shell will go into the background and return when you finish reading the manual page (or you can press q to quit rather than scrolling through the whole thing).

But say you're reading the manual page, and you want to do something else for a minute. No problem. Press Ctrl-z while you're reading to suspend the current foreground job and put the shell in the foreground. When you suspend a job, bash will first give you some information on it, followed by a shell prompt. You will see something like this on the screen:

NAME cp - copy files SYNOPSIS cp [options] source -More- 

[1]+ Stopped man cp

$

Note the last two lines. The next to last is the job information, and then you have a shell prompt.

bash assigns a job number to each command line you run from the shell. This allows you to refer to the process easily. In this case, man cp is job number 1, displayed as [1]. The + means that this is the last job you had in the foreground. bash also tells you the current state of the job - Stopped - and the job's command line.

There are many things you can do with jobs. With man cp still suspended, try the following commands:

man ls
Starts a new job.

Ctrl-z
Suspends the man ls job; you should see its job information.

man mv
Starts yet another job.

Ctrl-z
Suspends it.

jobs
Asks bash for a display of current jobs. The result looks like this:

{$} jobs 

[1] Stopped man cp

[2]- Stopped man ls

[3]+ Stopped man mv

{$}

Notice the - and +, denoting respectively the next to last and last foreground jobs.

fg
Places the last foreground job (man mv, the one with the +) in the foreground again. If you press the space bar, the man page will continue scrolling.

Ctrl-z
Re-suspends man mv.

fg %1
You can refer to any job by placing a % in front of its number. If you use fg without specifying a job, the last active one is assumed.

Ctrl-z
Re-suspends man cp.

kill %1
Kills off job 1. bash will report the job information, which will look like this:

$ kill %1

[1]- Terminated man cp

bash is only asking the job to quit, and sometimes a job will not want to do so. If the job doesn't terminate, you can add the -KILL5.1 option to kill to stop asking and start demanding. For example:

$ kill -KILL %1 

[1]- Killed man mv

The -KILL option forcibly and unconditionally kills off the job.

In technical terms, kill simply sends a signal. By default, it sends a signal that requests termination (TERM, or signal 15) but you can also specify a signal, and signal 9 (KILL) is the signal that forces termination. The command name kill is not necessarily appropriate to the signal sent; for example, sending the TSTP (terminal stop) signal suspends the process but allows it to be continued later.

top
This brings the top display back up. Give the u command in top to see only your processes. Look in the right-hand column for the man ls and man mv commands. man cp won't be there because you killed it. top is showing you the system processes corresponding to your jobs; notice that the PID on the left of the screen does not correspond to the job number.

You may not be able to find your processes because they're off the bottom of the screen; if you're using X (see Chapter 9 on page [*]), you can resize the xterm to solve this problem.

Even these simple jobs actually consist of multiple processes, including the man process and the pager more, which handles scrolling one page at a time. You may notice the more processes are also visible in top.

You can probably figure out how to clean up the remaining two jobs. You can either kill them (with the kill command) or foreground each one (with fg) and exit it. Remember that the jobs command gives you a list of existing jobs and their status.

One final note: The documentation for bash is quite good, but it is found in the Info help system rather than the man pages. To read it, type info bash. See section A.1.1 for instructions on using the info program. bash also contains a very good summary of its commands accessible by the help command. help displays a list of available topics; more information about each of them is accessible with the command help topic name. Try typing help cd, for example. This will give you details on the -P and -L arguments recognized by cd.


next up previous contents index
Next: A Few bash Features Up: The Basics Previous: The Shell   Contents   Index
John Goerzen / Ossama Othman