Creating and managing processes

The shell can also help you in running multiple programs at a time. Suppose you want to run a word count on a large file but you don't want to wait for it to finish. Then you can say:

wc hugefile > wc.output &

The ampersand & at the end of a command line says to the shell to take this command and start executing it in the background and get ready for further commands on the command line. If you don't redirect the output to a file it will show up on your terminal some time later when the wc program is done! The command jobs lists all background jobs that you have started from the current shell.

If you start a bunch of processes with the ampersand you can use the jobs command to list them. Then you can selectively bring one into the foreground by the command fg %n, where n is the job number as listed by the jobs command. You can also suspend a running program with Ctrl-z and put it in the background with the command bg.

Each running program is knon as a process. The number printed by the shell for each running program is a unique process-id that the operating system assigns to the process when it is created. To check for running processes use the ps command (ps is for process status). A sample session is shown below.

[alice@localhost]: date
Mon Oct 25 14:40:52 MDT 2012
[alice@localhost]: wordfreq Encyclopedia.txt  > output &
[1] 19027
[alice@localhost]: jobs
[1]+  Running                 wordfreq Encyclopedia.txt >output &
[alice@localhost]: ps
  PID TTY          TIME CMD
19018 ttyp1    00:00:00 bash
19027 ttyp1    00:00:00 wordfreq
19033 ttyp1    00:00:02 sort
19034 ttyp1    00:00:00 uniq
19035 ttyp1    00:00:00 sort
19036 ttyp1    00:00:00 wc
19037 ttyp1    00:00:00 ps
[alice@localhost]: 
[1]+  Done                    wordfreq Encyclopedia.txt >output
[alice@localhost]: date
Mon Oct 25 14:41:20 MDT 2012
[alice@localhost]:

To see all processes on the system, use the command ps augxw. To search for processes owned by a user bcatherm, use grep as shown below:

ps augxw | grep bcatherm

Here is a sample output

[alice@localhost alice]$ ps augxw | grep bcatherm
bcatherm 14322  0.0  0.0  6760 2020 ?        S    Dec01   0:02 /usr/sbin/sshd
bcatherm 14328  0.0  0.0  4396 1480 pts/14   S    Dec01   0:00 -bash
bcatherm 16659  0.0  0.0  6792 2032 ?        S    00:10   0:00 /usr/sbin/sshd
bcatherm 16667  0.0  0.0  4392 1472 pts/0    S    00:10   0:00 -bash
bcatherm 20128  0.0  0.0  4152 1072 pts/0    S    00:53   0:00 /bin/sh /usr/local/bin/pbsget -4
bcatherm 20134  0.0  0.0  1584  656 pts/0    S    00:53   0:00 qsub -v DISPLAY -q interactive -I /tmp/cluster.pbs.611
bcatherm 20140  0.0  0.0  4392 1472 ttyp0    S    00:53   0:00 -bash
bcatherm 20141  0.0  0.0  1456  312 ttyp0    S    00:53   0:00 pbs_demux
bcatherm 20818  0.0  0.0  1688  808 ttyp0    S    01:08   0:00 /usr/share/pvm3/lib/LINUXI386/pvmd3 -nws00
bcatherm 20901  0.4  0.0  8360 2868 pts/14   S    01:09   0:00 vim control/pvm/stage3.c
alice     20907  0.0  0.0  3592  628 pts/19   S    01:09   0:00 grep bcatherm
[alice@localhost alice]$

If you start a background job with the ampersand & and logout, normally the background job is terminated. However, you can ask the shell to let the background job continue running after you log out by using the prefix nohup. For example:

nohup mylongrunningprogram >& output &
logout

Next time you login, you can use the ps command to check whether the job has finished. Then check for the output file.