Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Job ID Versus Process ID

Background and suspended processes are usually manipulated via job number (job ID). This number is different from the process ID and is used because it is shorter. In addition, a job can consist of multiple processes running in series or at the same time, in parallel, so using the job ID is easier than tracking the individual processes.

Subshells

Whenever you run a shell script, in addition to any commands in the script, another copy of the shell interpreter is created. This new shell is known as a subshell, just as a directory contained in or under another is known as a subdirectory.

The best way to show this is with an example. I created a script known as psit and gave it execute permissions. This script runs ps and exits in the following example:

#! /bin/ksh
ps -ef | grep dhorvath
exit 0

When run, psit produces the following:

$ psit
 dhorvath 9830  3662  0 13:58:42 pts/6    0:00 ksh psit
 dhorvath 9831  9830 19 14:05:24 pts/6    0:00 ps -ef
 dhorvath 3662  3657  0 08:10:53 pts/6    0:00 -ksh
 dhorvath 9832  9830  0 13:58:42 pts/6    0:00 grep dhorvath
$

The subshell running as process 9830 is a child of process 3662, the original ksh shell. ps and grep are the children of process 9830 (ksh psit). When the psit script is done and exits, the subshell exits, and control is returned to the original shell.

You can also start a subshell by entering the shell name (ksh for Korn, sh for Bourne, and csh for C Shell). This feature is handy if you have one login (default) shell and want to use another. Starting out in Korn shell and starting C Shell would look like the following:

$ csh
% ps -f
     UID   PID  PPID  C    STIME TTY      TIME CMD
 dhorvath 3662  3657  0 08:10:53 pts/6    0:00 -ksh
 dhorvath 3266  8848 11 10:50:40 pts/6    0:00 ps -f
 dhorvath 8848  3662  1 10:50:38 pts/6    0:00 csh
%

The C shell uses the percent sign as a prompt. After the csh command starts the shell, the prompt becomes the percent sign. The ps command shows csh as a child process and subshell of ksh. To exit csh and return to the parent shell, you enter exit.

Process Permissions

By default, a process runs with the permissions of the user running it. In most cases, this makes sense, enabling you to run a command or utility only on your files. There are times, however, when users need to access files that they do not own. A good example of this is the passwd command, which is usually stored as /usr/bin/passwd. It is used to change passwords and modify /etc/passwd and the shadow password file, if the system is so equipped.

It does not make sense for general users to have write access to the password files; they could create users on the fly. The program itself has these permissions. If you look at the file using ls, you see the letter s where x normally appears in the owner and group permissions. The owner of /usr/bin/passwd is root, and it belongs to the sys group. No matter who runs it, it has the permissions of the root user.

Overlaying the Current Process (exec Command)

In addition to creating (forking) child processes, you can overlay the current process with another. The exec command replaces the current process with the new one. Use this command only with great caution. If you use exec in your primary (login) shell interpreter, that shell interpreter (ksh with pid 3662 in the previous examples) is replaced with the new process.

Using the command exec ls at your login shell prompt gives you a directory listing and then disconnects you from the system, logging you out. Because exec overlays your shell (ksh, for example), there are no programs to handle commands for you when ls finishes and exits.

You can use exec to change your shell interpreter completely without creating a subshell. To convert from ksh to csh, you can use the following:

$ exec csh
% ps -f
      UID   PID  PPID  C    STIME TTY      TIME CMD
 dhorvath  3662  3657  0 08:10:53 pts/6    0:00 csh
 dhorvath  3266  3662 11 14:50:40 pts/6    0:00 ps -f
%

The prompt changes and ps shows csh instead of ksh but with the original pid and start time.

Summary

In this chapter, you looked at the four major topics involving processes provided with the shell:

  Starting a process
  Listing running processes
  Killing a process (kill command)
  Parent and child processes

As you write scripts and use the shell, knowing how to work with processes improves your productivity.

Questions

1.  How do you run a command in the background?
2.  How do you determine what processes you are running?
3.  How do you change a foreground process into a background process?


Previous Table of Contents Next