Notes: Unix Lab 13

Introduction to Command Shells

  1. Stopping, Suspending a Job

    From the shell a running program is known as a process or "job". Typically, a job is started from the commandline and runs until the program completes its task. While a program is running, the shell at the current terminal will not respond to any further commands until the job terminates. A program run in this way is said to be running in the "foreground".

    A foreground job may be forefully terminated by hitting the control key and the "c" key simultaneously. This can be shown as "<ctrl>-c" or "C-c". Terminating a process in this way be be useful if it is taking an inordinate amount of time to complete, if the program crashes or hangs for some reason or any other reason that the user does not want to run to completion.

    Another way of managing a foreground job is to suspend it. A job may be suspended by entering "<ctrl>-z" at the controlling terminal. When this is done, the shell will print "Suspended" and resumes control of the terminal so that the user may use it to enter additional commands. The suspended job is still "running" on the system but is essentially frozen in its current state until it is resumed.

  2. The jobs command

    To list all jobs that are running from the current shell, the jobs command may be used. The output shows the number of the job ID in the first column. The second column shows the state that the job is in (Suspended or Running). The third command shows the actual command for the job.

     
        
    > 
    > xclock
    
    Suspended
    >
    > find /usr -name \*.xpm
    
    Suspended
    >
    > jobs
    [1]  - Suspended                     xclock
    [2]  + Suspended                     find /usr -name *.xpm
    > 
    > 
    

  3. Restarting a Job

    If a job is suspended, it may be resumed by entering the command fg. The fg command will bring the suspended job to the foreground and allow it to continue processing. If more than one job is suspended the most recent program will be brought to the foreground.
     
              
    >
    > find /usr -name \*.xpm | tail -f
    
    Suspended 
    > jobs
    [1]  + Suspended                     find /usr -name *.xpm | tail -f
    > xclock 
    
    Suspended
    > fg
    xclock
    
    > 
    

    When there are multiple suspended (or backgrounded, see below) jobs you may specify the job to foreground by adding an argument to the fg command.

  4. Running a Job in the Foreground

  5. Running a Job in the Background

    Running a job in the background is similar to suspending a job except that the job continues to process data and will run until it completes its task or is terminated by the user. When a job is put in the background control of the shell is given back to the user. There are a number of ways of running a program in the background:

    A job may be started so that it will run in the background by ending the command with the "&" character.

     
    > 
    > xclock &
    [3] 7738
    > 
    
    When a command is started in the background it will display the job ID number in brackets and its process ID number (more on that below).

    A suspended command may be resumed to run in the background by entering the bg command. If more than one job is suspended or backgrounded it may be resumed in the background by specifying its job number as an arguemnt to the bg as shown below:
     
    > 
    > jobs
    [2]  + Suspended                     xclock
    [3]    Running                       xclock
    [4]  - Suspended                     man tcsh
    > 
    > bg %2
    [2]    xclock &
    > jobs
    [2]    Running                       xclock
    [3]  - Running                       xclock
    [4]  + Suspended                     man tcsh
    > 
    

    Here the xclock job (ID #2) is in a suspended state. Job ID #2 is then run in the background which the jobs command shows as "Running" in the background.

  6. Monitoring and Listing Current Jobs

  7. killing jobs with the kill command

    The kill command can be used to terminate a process. The process to terminate may be specified by its process ID (PID) or by the shell's job ID.

    The kill command using the job ID.
     
    tuckerm@apollo:~> 
    tuckerm@apollo:~> jobs
    [1]  + Suspended (signal)            top
    [2]  - Running                       xclock
    tuckerm@apollo:~> kill %2
    tuckerm@apollo:~> 
    [2]    Terminated                    xclock
    tuckerm@apollo:~> 
    

    The kill command using the process ID.
     
    tuckerm@apollo:~>
    tuckerm@apollo:~> ps auxw |grep xclock
    tuckerm  26212  0.1  0.1  4404 2488 pts/1    S    03:54   0:00 xclock
    tuckerm  26215  0.0  0.0  1468  456 pts/1    S    03:54   0:00 grep xclock
    tuckerm@apollo:~> kill 26212
    tuckerm@apollo:~> 
    [2]    Terminated                    xclock
    tuckerm@apollo:~>