CIS Header Left CIS Header Center
C-2-100 Mackinac Hall
1 Campus Drive
Allendale, MI 49401

Phone:616-331-2060
Fax:616-331-2106
email: info@cis.gvsu.edu


ABET Accredited Symbol

The Computer Science and Information Systems programs are accredited by the Computing Accreditation Commission of ABET.
Find us on Facebook

Find us on Twitter
Job Control

Index->Job Control

Job Control

Overview and Goals

At the completion of this lab, you will be able to...

  1. kill a process using Ctrl-C
  2. run processes in the background
  3. move a process from the background to the foreground
  4. suspend a running process
  5. move a process from the foreground to the background
  6. generate a list of running processes
  7. kill a running process
  8. send the output of a command to a file
  9. send a file to a command as input
  10. send the output of a command to another command

The following utilities are used in this lab: bg(1), fg(1), jobs(1), kill(1), ps(1),

Special Keys

The shell assigns special meaning to several keystrokes. One useful one is "Ctrl-C". If you are running a program and it has hung, or is otherwise taking too long, you can use "Ctrl-C" to kill it. For example, try the following: "cat". cat is sitting there waiting for you to give it some data. Assuming you change your mind and want to kill the program, hit "Ctrl-C" and you will be returned to the shell prompt.

Background Processes

A "process" is a program that is currently running, in contrast to a "program", which is a file on disk that contains executable code.

In Linux, when you run a program it typically runs in the "foreground", i.e., the shell waits for the process to complete before you can type in the next command. If you know a process is going to take a while to run, you can run it in the "background". Running in the background means that you will get the shell prompt back immediately, so you can continue working. At the same time, your process will be running.

To run a process in the background, simply append an ampersand "&" to your command. For example,

find / >& /dev/null &
will run the find command in the background. (Don't worry what the command does for now. Trust me, it will take a while to run.) If you try this, you will see output something like:
[1] 19639
and you will see the shell prompt immediately. The number in the bracket tells you that this is the first background job given to the shell, and the second (longer) number is the process ID of the jobs. While the process is busy in the background, you can now continue working.

If you want to kill the background process, there are many ways of doing it. One way is to first move the process into the foreground again, then hit "Ctrl-C". To move the process back into the foreground, run the "fg" command. This moves the previously backgrounded command into the foreground where you can interact with it (like typing "Ctrl-C" to kill it).

Suspending Processes

What if you run a program in the foreground, then decide it's taking too long and want to move it into the background. You must first suspend the process using "Ctrl-Z". Run the "find" command from above, but leave off the "&" so it will be run in the foreground. Now hit "Ctrl-Z" and you should see something like:

[1]+ Stopped find / >/dev/null
This line is telling you that your command is currently stopped (i.e., suspended).

To continue to run it to the background, use the "bg" command. bg moves the just-suspended process into the background.

Listing Processes Owned by the Shell

There are several ways to see what processes are currently running. The simpliest is by running "jobs". jobs will list all of the background processes associated with your shell. Run the find command again in the background. Now type

jobs
You should see something like:
[1]+ Running find / >/dev/null &

Jobs are sequentially numbered from 1, so unless you have other processes in the background, find should be the one and only job you have, and thus it is numbered 1 ("[1]").

The fg and bg commands take a job number as an argument, to allow you to move specific jobs to the foreground and background. For example, "fg %2" will move the 2nd job into the foreground. Note the percent sign before the job number.

Listing All Processes

jobs displays background processes that are run from your current shell. If you have several shells active, each running different programs, you must use a different utility to get a list of all of your processes.

The ps command is used to give a list of processes. It can even show you all of the processes running on the system, even those that aren't owned by you. Typing "ps" will show you processes owned by the current shell, but in a slightly different format than jobs shows. Typing "ps -A" will give you a list of all processes running on the system.

Every running process has a unique "process id" or "pid". Unlike job numbers that are local to the shell, pids are unique system-wide. No two processes on the same machine have the same pid. The left-most column of "ps -A" is the pid.

Killing A Running Process

You can kill any process that you own by using the kill command. The format is "kill pid".

Run the find command once again in the background. Type "ps" to see your list of processes. Find the pid of find and kill it using the kill command.

Redirection

Processes typically take input from the keyboard (standard input) and send output to the screen (standard output). However, you can change that behavior with "redirection" using special characters that the shell understands.

Input redirection allows you to input data to a command from a file, rather than having to type it in on the keyboard. The format is "command < filename". Try this command

wc < /etc/passwd
wc stands for "word count" and can count the characters, words, and lines in a file. This command feeds /etc/passwd into wc, so you should see displayed the number of lines in the file.

Output redirection allows you to capture the output of a program to a file, rather than having it displayed on the screen. The format is "command > filename". Try this command

      ps -A > ps.out
You should see no output on the screen. The output went to a file in the current directory, ps.out. Type "cat ps.out" to see the output of ps.

You can use both input and output redirection simultaneously. For example, "wc < /etc/passwd > wc.out" will run wc on /etc/passwd and capture the output to wc.out.

Every Unix process has three file descriptors automatically open for them: stdin, stdout, and stderr (standard input, output, and error). Among these three, stdout and stderr are used for outgoing data flow from a process, while stdin is for incoming data flow to a process. The aforementioned method for output redirection does not capture output produced from stderr. For more information, read the bash(1) manual page.

Piping

Pipes allow you to send the output of one command into another command. This is one of the most useful aspects of the Linux command-line. The general format is "command1 | command2 | command3 ...". This pipe symbol "|" connects

  • the standard output of command1 to the standard input of command2,
  • the standard output of command2 to the standard input of command3,
  • ...

Pipes allow you to create a "pipeline" of functionality out of existing utilities. For example, to count the number of processes currently running, you could type "ps -A | wc ".

Questions/Exercises

Turn in your answers to the following questions:

  1. From a shell window, run the Konqueror Web Browser in foreground by typing konqueror. (You must run konqueror this way, not by clicking the icon on the task bar or from menu item from the Start menu)
    • Put the running konqueror in background (Press Ctrl-Z, and then type "bg"). What is the output you see on the shell window?
    • What is the output you see after typing the following command "ps -C konqueror"?
    • Close konqueror and type an empty command on the shell (just press Enter key once). What is the output you see on the shell window?
  2. Look at the man page of "ps" and search for "STANDARD FORMAT SPECIFIERS"
    • Run the following command
      ps -o pid,ppid,cmd -u your_user_id
      What are the last three lines of output?
    • Now run the same command by adding the -H option.
      ps -H -o pid,ppid,cmd -u your_user_id
      How does the -H option change the output? (By the way, do you know that you can use the arrow keys to recall and edit previously typed commands)
  3. How would you use the tail command in a pipe to obtain the last six lines of output from the previous ps command?
  4. Which pipe of commands would you use to count the number of processes owned by you only? Run the command and count how many processes you own? Verify your answer!

Top

This page was last modified March 28 2006.
Send comments to the CIS School Webmaster.
Copyright ©1995 - 2009 Grand Valley State University.
GVSU is an Equal Opportunity/Affirmative Action Institution.