<<

. 23
( 69 .)



>>


TYPE THIS: RESULT:
[ferro:˜] user% man kill
The process is killed.
control+Z
[ferro:˜] user% kill “KILL <type the process id>
[ferro:˜] user% jobs




– The man page appears. ¤ In the original Terminal › Type ps again and press
KILL A PROCESS BY NAME
window, type ps and press Return.
„ In a new Terminal Return.
– Your man command is
window, type man killall
– Your man command
and press Return. gone.
appears in the process list.
– The man killall
‹ Type killall man and press command terminates.
Return.
103
UNIX FOR MAC



LIST ACTIVE PROCESSES
The ps “a command lists all users' processes. You will

Y
ou can use the ps command to find out what
own some of these processes, while others start when the
processes are running on your system or to find the
system boots.
process ID for a job that you want to kill. To kill a
process by its process ID, you obviously must know its
The ps “u command adds information such as when each
process ID. Fortunately, Unix systems provide a command
process started, as well as the how much CPU and memory
that displays information about running processes, including
each process is using.
the process IDs. That command is ps.
The ps “x command adds processes that are not associated
You can also use the ps command to display a list of all
with a particular Terminal window, such as the processes
processes that are running on your system. Depending on
started before you logged on.
the arguments that you provide, this list can include
information about the time each process started and who The most commonly used ps command for Mac OS X users
started each process. is ps “aux, combining the most useful command options.
The ps command by itself only displays a list of the By using the ps command followed by a vertical bar,
commands you are running. To list system commands commonly referred to in Unix as a pipe, you can restrict
and the commands that other users are issuing, you add the output that it return to your specific interests. For
arguments to your ps command. The ps -aux command example, ps “aux | grep init displays the init process
shows all processes running on the system and provides and other processes that contain this string in their names.
details on each one.

LIST ACTIVE PROCESSES




– The screen displays a list – The screen displays a
„ In a Terminal window, ¤ Type ps “u and press
type ps and press Return. of the processes you are Return. detailed list of the processes
running. you are running.




104
7
WORK WITH PROCESSES



The columns in the output of the ps -aux command provide a lot of information
about running processes. This information can help you understand who is using
your system, what is running, and sometimes why the systems is running slowly.
Each of these columns is explained in the table below.

PS COLUMN MEANING
Username of the person running the process.
USER
Numeric process ID.
PID
Percentage of CPU resources the process is using.
%CPU
Percentage of memory the process is using.
%MEM
Virtual size of the process in kilobytes.
VSZ
Size of process in memory.
RSS
Associated control terminal, or ?? if there is no associated terminal.
TT
Status of the process.
STAT
Time, if started today, or date a process was started.
STARTED
Accumulated run time.
TIME
Command that is running, complete with arguments.
COMMAND




– The screen displays
‹ In the Terminal window, ˇ Type more and press Á Type ps “aux | grep tcsh
type ps -aux and a space. Return. and press Return. information for each tcsh
process, plus the grep
– The screen displays details
› Type a vertical bar (|) and command used to select
a space. for all processes running on these processes.
the system.


105
UNIX FOR MAC




MONITOR THE TOP PROCESSES
the ps command displays. In addition, the top command

Y
ou can use the top command to find out which
provides useful information about processes in general and
processes are using the most resources on your
system performance.
system. This knowledge is especially useful when
your system is running slowly. Although processes appear
The information included in the top output for each process
to be running simultaneously, they actually take turns using
includes the process ID (PID), the simple command without
the CPU. This sharing happens at an extremely rapid rate,
arguments, and the percentage of CPU usage. It also includes
but you can view some process details using commands
information on threads and memory sizes.
such as ps and top.
Some of the information the top command displays tells
While the ps command allows you to view important
you a lot about system performance. For example, if a
statistics related to processes ” such as how long they have
system is more than 90 percent idle, you know that the
been running or how much memory they are using ” the
system is not running more processes than it can handle
ps output display order is somewhat random. To examine
and you can assume that performance is good. Another
processes to determine how much demand they are placing
good indicator of performance is the system load. The top
on the system, another tool is more appropriate. That tool
command has numerous options that you can use to alter
is top.
its behavior. For more information on these options, type
man top and press Return.
The top command orders its output to display the tasks
using the bulk of the CPU time available at the top of the
list. The columns in the top output are similar to those that

MONITOR PROCESSES WITH TOP




– The top command – The top command stops
„ In a Terminal window, ¤ Press Control + C.
type top and press Return. displays information about processing.
processes.




106
7
WORK WITH PROCESSES




You can use the top command to determine how hard your
system is working. The load averages that the top command
displays tell you how many jobs, on average, were ready to run
but were waiting for access to the CPU. Small load averages like
those shown in the top output support the conclusion that the
CPU on this particular system is not busy and that processes
rarely have to wait for access to it. Load averages above four may
indicate a system that is burdened. Load averaging higher than
10 indicates a system with excessive CPU contention.
You can use the uptime command to see how many users are
logged on and to display load averages. This command tells you
how long the system has been up, how many users are currently
logged on (though it counts each Terminal as a separate logon),
and the 1-minute, 5-minute, and 15-minute load averages.

RESULT:
TYPE THIS:
2:26 PM 4:18, 3 users, load
uptime
averages: 0.03, 0.03, 0.01




– The top command
‹ Type top “e and press
Return. displays processing events
for the top processes.




107
UNIX FOR MAC




WRITE A SIMPLE SHELL SCRIPT
command in a file and execute that file, you also get a

Y
ou can place commands that you repeatedly execute
listing of your files. While there is no advantage to
in a file and execute these commands by entering the
executing a simple command such as ls using a script, the
name of the file. A file of Unix commands that you
advantage to scripting becomes readily obvious when you
execute is called a shell script. Writing shell scripts can save
need to execute complex commands or many commands in
you a lot of work and make it unnecessary for you to
a certain order. In fact, script writing is so efficient that
remember complicated commands.
nearly everyone who manages a Unix system automates
routine tasks by writing scripts.
The Unix commands that you place in a script are
commands that you might have entered in a Terminal
You can write shell scripts that ask the person running them
window. When you run the script, the system executes the
to supply some information or that make use of the user's
commands in the order entered. Shell scripting is a very
shell variables. For example, one of the simplest shell scripts
basic form of Unix programming, although scripts can run
you can write greets a user when he or she executes it. If
the gamut from a simple list of commands to elaborate
you place the command echo hello, $USER in a file, the
programs with looping, embedded functions, and complex
user running the script sees a personalized message such as
data structures.
"hello, jdoe."
If you enter a command such as ls in a Terminal window,
the shell passes the command to the kernel for execution.
The result is a listing of your files. If you enter the same




WRITE A SIMPLE SHELL SCRIPT




– The Pico text editor opens.
„ To start the Pico text ‹ Type hello, $USER.
editor, type pico and press
– The shell variable $USER

<<

. 23
( 69 .)



>>