<<

. 22
( 69 .)



>>



97
UNIX FOR MAC




RESTART A PROCESS
Job numbers are always small numbers like 1 or 4. While

Y
ou can save some work by restarting a suspended
you can accumulate a large number of suspended jobs if
process. Instead of running the job again, you can
you work at it, in practice this is never done. Process IDs are
pick up where you left off when you suspended the
usually large numbers like 325 or 5234. The system assigns
process. In addition, restarting a suspended process frees
these numbers in some fashion, reusing the numbers as
resources tied up by that process.
needed. If you see a process running today with the process
ID 4321, you might not see this number used again for
All processes that you run, suspended or not, are associated
months. Process numbers that are small like 1 or 69 are
with the particular shell in which they were started. If you
assigned to system tasks.
suspend a single process in each of two Terminal windows,
for example, each of these processes will have 1 as its job
To restart a suspended process, use the fg (foreground)
number. At the same time, each of these suspended
command. If only one suspended process is in your current
processes will also have a unique process ID. Where each
shell, that process restarts. If there are several suspended
job number is related to the parent shell and only the
processes, the one most recently suspended will restart. To
parent shell, each process ID is related to the entire system.
restart a particular suspended process, enter the fg
This means that you can refer to a process by its job
command followed by its job number.
number only in the shell in which it was started.



RESTART A PROCESS




– The system displays a list – The most recently
LIST SUSPENDED PROCESSES RESTART THE LAST
SUSPENDED PROCESS
of suspended processes with suspended process begins
„ Type jobs and press
„ Type fg and press Return.
their job numbers enclosed in where it left off.
Return.
square brackets.




98
7
WORK WITH PROCESSES



You can only restart a process that has been suspended. You
cannot restart a process that has run to completion or a
process you have terminated. Each process has a life of its
own. Even if you run the same command seven times in a row,
each running is an independent process.
You can restart processes by bringing them back into the
foreground, and they will then continue running as if they had
not been suspended. You can also restart processes by sending
them to the background and continue entering commands
from the shell from which these processes were run.
If you press Control + D to exit a shell in which you have
suspended processes, the shell alerts you to the suspended
processes. The message "There are suspended jobs" appears.
This warning ensures that you do not forget about commands
that you suspended. You can still exit the shell by pressing
Control + D again. If you exit the Terminal window by clicking
the red button, no alert appears. The suspended processes are
quietly killed when you close the window.




– The system displays a list – The process that was
¤ Type fg %1 and press
RESTART A PROCESS
WITH ITS JOB NUMBER of suspended processes with Return. suspended first starts running
„ Type jobs and press their job numbers enclosed in again.
square brackets.
Return.




99
UNIX FOR MAC



RUN A PROCESS IN THE BACKGROUND
command, type an ampersand at the end of the line, and

R
unning a command as a background process allows
press Return, the find command continues processing and
you to continue entering commands while that
writes to your screen as it locates files that meet your
process runs. A process running in the background is
search criteria. At the same time, you have control of the
not suspended; it continues to execute. When you run a
command line and can issue other commands while the
command in the Terminal application, the output from the
find command continues to run.
command appears on your screen. Until the command
finishes, no other commands can run. This is called
Like suspended processes, jobs that run in the background
foreground processing. When the command finishes
have both a job number and a process ID. Both suspended
running, your command prompt returns.
processes and background processes appear in the listing
when you use the jobs command. The important
Unix allows you to run commands in the foreground or in
difference between a suspended process and a background
the background. Unlike commands in the foreground,
process is that a background process continues to run while
commands run as background processes allow you to enter
a suspended process is inactive.
additional commands. Your shell continues prompting and
you can continue entering commands.
Running commands as background processes allows you to
get more work done in a single Terminal window. If you
The simplest way to run commands in the background is to
press Control + D to exit a shell while there are background
start them in the background. You can run a command in
processes running, the system issues a warning.
the background by adding an ampersand (&) to the end of
the command line. For example, if you enter a find


RUN A PROCESS IN THE BACKGROUND




– A message displays – The find command
„ Type find / -name fg “print ¤ Type date and press
& and press Return. indicating that your find Return. continues running in the
command cannot search background, while you enter
– The job number and some directories. the date command and get
process ID of the background your output.
process appears.


100
7
WORK WITH PROCESSES




You can place a process into the
background by first suspending it TYPE THIS:
with Control + Z and then using
find / -name findme “print
the bg (background) command to control+Z
move it into the background. If you bg
run a command, such as find / pwd
date
-name findme -print, and then
suspend it by pressing Control + Z,
you can restart it in the background
with a bg command, such as bg RESULT:
%3. The command continues
running and searching for your files /Users/user
Thu May 1 11:12:13 EDT 2003
while allowing you to enter other
commands at the shell prompt.




– The find command from – The output lists the
‹ Type !find and press › Type jobs and press
Return. your command history runs Return. running and suspended
again. processes.




101
UNIX FOR MAC



KILL A PROCESS
if they are running in other Terminal windows. When you

Y
ou can use the kill command to stop a running or
issue the killall command, the system looks for
a suspended process. By halting a command that you
commands you are running.
mistyped, you can avoid wasting system resources or
performing a process that you did not intend to run.
As a normal non-root user, you can only kill processes that
you have started. As a root user, however, you can kill any
Stopping a process that is running on a Unix system is
process on the system. Unix provides little protection from
called killing it. To kill a running process, you use the kill
making mistakes that can bring your system down. One of
command followed by the process ID of the process. For
the worst mistakes that root users can make on a Unix
example, kill 1234 would kill the process with process
system is to enter kill 1 when they mean to enter kill
ID 1234. To kill a suspended process, you use the kill
%1. The user's intention is to kill a suspended process. Typing
command followed by the job number. For example, kill
the command without the % kills a process known as init
%2 would kill the second suspended process.
and will likely crash your system. For this reason, most
disciplined system administrators avoid using the root
After you kill a process, you cannot restart it. You can run the
account except when there is no other way to accomplish
same command again, but this action starts a new process.
a job.
You can kill a process by name. The variant of the kill
command used for this purpose is called killall. If you
enter killall man, you will kill all man commands, even

KILL A PROCESS




¤ Type kill followed by a ¤ Type kill followed by
KILL A PROCESS KILL A PROCESS BY PROCESS ID
BY JOB NUMBER job ID number, such as %2, a process ID from the list
„ Type ps and press Return.
„ Type jobs and press and press Return. displayed and press Return.
Return. – The second suspended – The second suspended
process terminates. process terminates.



102
7
WORK WITH PROCESSES



You can use the kill command to send signals to running processes. The kill
command is useful for terminating a process. Although most people talk about the
kill command as if its sole function were to terminate processes, this is not the
case. In fact, the kill command's real function is to send very short messages
called signals to running processes to control the behavior of these processes. The
default kill command ” the command without an argument ” sends a signal
that asks the process to terminate. Systems people often refer to the default signal
as a SIGTERM. Another common signal, referred to as a SIGHUP, for hang up, is
most often used to tell a running process that it should go back and re-read its
configuration file. Many system processes remain unaware that their configuration
files have changed unless you send this signal to them. Another often-used signal
is SIGKILL, a signal used to terminate a process that is not terminated with
SIGTERM.

<<

. 22
( 69 .)



>>