4.5 Managing processes with Bash

http://tldp.org/LDP/www.debian.org/doc/manuals/debian-tutorial/ch-basics.html

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 apipeline --- 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 type 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. Type C-z while you're reading to suspend the currently foregrounded job, and put the shell in the foreground. When you suspend a job, Bash will first give you some information on it, and then a shell prompt. You will see something like this on the screen:

     NAME
            cp - copy files
     
     SYNOPSIS
            cp [options] source dest
            cp [options] source... directory
            Options:
            [-abdfilprsuvxPR]  [-S backup-suffix] [-V {numbered,exist­
            ing,simple}]   [--backup]   [--no-dereference]   [--force]
            [--interactive] [--one-file-system] [--preserve] [--recur­
            sive]  [--update]   [--verbose]   [--suffix=backup-suffix]
            [--version-control={numbered,existing,simple}] [--archive]
            [--parents] [--link]  [--symbolic-link]  [--help]  [--ver­
            sion]
     
     DESCRIPTION
     --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 this:

  1. man ls

    Start a new job.

  1. C-z

    Suspend the man ls job by pressing Control and lowercase z; you should see its job information.

  1. man mv

    Start yet another job.

  1. C-z

    Suspend it.

  1. jobs

    Ask Bash for a display of current jobs:

         $ jobs
         [1]   Stopped                 man cp
         [2]-  Stopped                 man ls
         [3]+  Stopped                 man mv
         $
    

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

  1. fg

    Place the last foregrounded job (man mv, the one with the +) in the foreground again. If you press the spacebar, the man page will continue scrolling.

  1. C-z

    Re-suspend man mv.

  1. 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.

  1. C-z

    Re-suspend man cp.

  1. kill %1

    Kill off job 1. Bash will report the job information:

         $ 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 -9 option to kill to stop asking and start demanding. For example:

         $ kill -9 %1
         [1]-  Killed                  man mv
         $
    

    The -9 option forcibly and unconditionally kills off the job. [8]

  1. top

    Bring 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 since 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, 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 a page at a time. You may notice themore 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 will tell you the list 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 Using info, Section 5.2 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 informations about each of them being accessible with the command help topicname; Try to type

     help cd

for example. This will give you details on the -P and -L arguments recognized by cd.


你可能感兴趣的:(shell,kill,command,bash,documentation,jobs)