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.
jobs
commandjobs
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 > > |
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.
A job may be started so that it will run in the background by ending the command with the "&" character.
|
||||
> > xclock & [3] 7738 > |
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 > |
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.
ps
commandps
command is used to list the process
status for some or all of the processes running on the
computer, not just the terminal as was the case with the
jobs
command. The actual output of ps
varies depending on the type of Unix being run. Generally this
breaks down between Unix versions based on the BSD and those
based on AT&T's System V Unix.
By itself, ps will show just the processes the user is running.
|
||||
mark@platypus:~> ps PID TTY TIME CMD 31852 pts/10 00:00:00 bash 3206 pts/10 00:00:00 tcsh 3228 pts/10 00:00:00 ps mark@platypus:~> |
ps
with the options auxw.
e - show process for all users
f - show full listing for each process
u - show username associated with the process
x - show processes that do not have a controlling tty
w - wide output
|
||||
mark@platypus:~> ps euxw |head -5 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 484 64 ? S Jan28 0:04 init [4] root 2 0.0 0.0 0 0 ? SW Jan28 0:04 [keventd] root 3 0.0 0.0 0 0 ? SWN Jan28 0:00 [ksoftirqd_CPU0] root 4 0.0 0.0 0 0 ? SW Jan28 0:40 [kswapd] mark@platypus:~> |
ps
with the -ef options
e - all users
f - full listing
|
||||
mark@platypus:~> ps -ef |head -5 UID PID PPID C STIME TTY TIME CMD root 1 0 0 Jan28 ? 00:00:04 init [4] root 2 1 0 Jan28 ? 00:00:04 [keventd] root 3 1 0 Jan28 ? 00:00:00 [ksoftirqd_CPU0] root 4 1 0 Jan28 ? 00:00:40 [kswapd] |
top
provides a continuously updated display of
the processes running on a system. To exit top hit the
"q" key. A brief help for top
's
interactive commands can be found by hitting the
"h" key.
|
||||
22:20:31 up 12 days, 1:42, 2 users, load average: 0.00, 0.00, 0.00 53 processes: 51 sleeping, 1 running, 0 zombie, 1 stopped CPU states: 2.4% user 14.1% system 0.2% nice 0.0% iowait 83.1% idle Mem: 515484k av, 441568k used, 73916k free, 0k shrd, 79924k buff 100092k active, 234592k inactive Swap: 473908k av, 7976k used, 465932k free 173840k cached PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME CPU COMMAND 14221 mark 14 0 1060 1060 812 R 1.9 0.2 0:00 0 top 1 root 8 0 80 68 48 S 0.0 0.0 0:06 0 init 2 root 9 0 0 0 0 SW 0.0 0.0 0:00 0 keventd 3 root 19 19 0 0 0 SWN 0.0 0.0 0:00 0 ksoftirqd_CPU 4 root 9 0 0 0 0 SW 0.0 0.0 0:08 0 kswapd 5 root 9 0 0 0 0 SW 0.0 0.0 0:00 0 bdflush 6 root 9 0 0 0 0 SW 0.0 0.0 0:00 0 kupdated 10 root -1 -20 0 0 0 SW< 0.0 0.0 0:00 0 mdrecoveryd 11 root 9 0 0 0 0 SW 0.0 0.0 0:07 0 kjournald 172 root 9 0 0 0 0 SW 0.0 0.0 0:00 0 khubd 989 bin 9 0 576 572 496 S 0.0 0.1 0:00 0 rpc.portmap 995 root 9 0 608 608 528 S 0.0 0.1 0:00 0 syslogd 998 root 9 0 460 460 404 S 0.0 0.0 0:00 0 klogd 1001 root 9 0 428 424 384 S 0.0 0.0 0:00 0 inetd 1004 root 9 0 1192 1120 1020 S 0.0 0.2 0:00 0 sshd 1021 root 9 0 1684 1660 1192 S 0.0 0.3 0:05 0 cupsd |
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:~> |