Linking processes

Last revision August 9, 2004

Table of Contents:

  1. Shell interpretation
  2. Editing command lines
  3. Stuck in a Unix login session?
  4. Program execution
  5. Simple commands
  6. Unix command syntax
  7. Controlling processes
  8. Your login environment

Processes can be linked together in various ways.

  • A pipe symbol (|) between program names tells the shell to start two child processes simultaneously (one for each program) and arrange for the standard output of the first to be directed to the standard input of the second. These "pipelines" can extend to multiple processes. Example:
          w | sort | more
    Pipelines are discussed in detail in the section on standard files and pipes.
  • A semi-colon (;) between program names tells the shell to run them in sequence. Start the first command. When it finishes, start the second. Either "command" may be the name of a single program or an entire pipeline. Example:
          lpr temp; rm temp
  • The ampersand (&), used to indicate that a process should run in the background, can also be used to separate two processes on the typed input line. The first goes into the background, the second to the foreground. Both are started "simultaneously" (actually, there is a micro-second delay to get the background one going before the foreground one is started). For example:
          bigjob & ls -l
  • In addition to sequential execution using the semi-colon, you can conditionally execute commands (simple or pipelines) in sequence using these metacharacters between the commands:

          &&    Execute the second command only if the first succeeds.

          ||    Execute the second command only if the first fails.

    For example, you want to run your big Fortran job bigsim in the background, where it could take several hours to complete. You want the system to send you email if the job fails (returns an error code). Conditional execution is ideal for this, but remember that both the main and conditional processes are part of the same "job", and the entire job has to be put into the background by putting the ampersand at the end of the whole command line. This example should be typed all on one line.
          bigsim > bigsimout || mailx -s "bigsim failed\!" < /dev/null &
    There are some peculiarities in this example. The exclamation point in the subject line for the mailx must be escaped, or the C-shell will think you am trying to invoke command retry. mailx wants you to type in my message body at the terminal, but will only run if bigsim fails, so you can't type the message immediately and must provide a pre-existing message in a file. You don't really need a message text, just the subject line, so you redirect the standard input of mailx to the system file /dev/null which means "no input".

  • Parentheses can be used to group piped or sequential commands to insure proper interpretation of connections, or to localize effects of commands. The grouping is done by forking off a sub-shell (another process running the shell program) and passing off the commands that were inside the parentheses for that sub-shell to interpret and run. Here is an example:
          cd fromdir; grep 'farrell' psout | (cd todir; cat > psfarrell)
    Here, the cd fromdir command is executed first in the main shell (remember that cd is a shell built-in command, not a separate program). Next, the grep command is executed as part of a pipeline. The second half of the pipeline is a sub-shell. This sub-shell first executes the cd todir command to change its working directory, then runs the cat program, which will copy the pipeline input to a new file. Commands in parentheses have the same environment, which can differ from other commands or the login shell.

A set of linked processes (using | ; && || or parentheses) is considered to be a single "job" by the C-shell. All processes in the job are either in the foreground together, or the background together, or suspended together. You start the whole job in the background with an ampersand as the last character of the entire command line that you type.

<--Previous Overview Next-->

Comments or Questions?