Section 3.2. Process Management

3.2. Process Management

Each command you invoke or program that you run from a Terminal window becomes a child of that terminal's shell. The Terminal window can juggle many child processes at once, but only one at a time is brought to the foreground, writing its output (through the Unix standard output file handle) to the Terminal, and accepting keyboard input (via Unix standard input) from the user . Any other processes are either placed in the backgroundrunning but not displaying any interface or accepting inputor suspended ( paused ) in the process of execution.

You can control the application in the foreground by sending it Unix signals via the keystrokes listed in Table 3-1. Programs usually respond to them as listed, though individual programs may interpret them differently. (The Emacs text editor, for example, ties a text-searching function to the Control-S keystroke.)

Table 3-1. Foreground process control keystrokes

Keystroke

Description

Control-C

Sends an interrupt signal, which usually causes the program to exit.

Control-D

Sends an end-of-file signal. If a program is accepting multiple lines of input from you, this signals that you're finished providing it.

Control-Z

Suspends the process in the foreground, and returns you to the command line.

Control-S

Suspends the process in the foreground, but keeps it in the foreground.

Control-Q

Resumes a suspended process, and brings it to the foreground.


Control-Q is a good keystroke to try if a Terminal window ceases to update or accept input for no obvious reason, while other Terminal windows continue behaving normally. You may have hit Control-S by mistake.

Terminal aliases Control-C to the File Send break (Control-C or .) menu selection. is a legacy Mac keyboard shortcut for interrupting programs; it's often used to quickly invoke the Cancel button in dialog windows.


Table 3-2 lists some Terminal commands that are useful for viewing and controlling backgrounded processes . You can find complete references for them in Chapter 2. See the next section to find out a process's process ID number (PID), which many of these commands require. (You can also use the more convenient %N syntax described by that section when working with processes that are children of the current Terminal window's shell.)

Table 3-2. Process control commands

Command

Description

ps

Lists Terminal-based processes belonging to you.

jobs

Lists processes that are children of this terminal's shell process.

fg pid

Foregrounds (and resumes, if suspended) the process with that pid .

bg pid

Backgrounds (and resumes, if suspended) the process with that pid .

kill - signal pid

Sends a signal (the terminate signal, by default) to the process with that pid .

killall - signal process- name

Sends a signal (the terminate signal, by default) to all processes with that name.


3.2.1. Seeing processes

Typing ps by itself displays a simple list of all the shells you are running, as well as all their child processes:

 andy@honey[~/mosxnut3]$ ps PID  TT  STAT      TIME COMMAND  1692  p1  S      0:00.35 -bash  3273  p2  S      0:00.02 -bash  3284  p2  S+     0:00.75 ssh chimpy  3311  p3  S      0:00.02 -bash  3313  p3  S+     0:00.05 vim outline.pod  3883  p4  S      0:00.02 -bash  3886  p4  S+     0:00.10 make perl  3950  p4  S+     0:00.01 cc -c -DPERL_CORE -fno-common -DPERL_DARWIN -no-  3958  p4  S+     0:00.01 powerpc-apple-darwin8-gcc-4.0.0 -c -DPERL_CORE -  3959  p4  R+     0:00.07 /usr/libexec/gcc/powerpc-apple-darwin8/4.0.0/cc1 -  3960  p4  S+     0:00.00 as -arch ppc -o op.o  3961  p4  S+     0:00.00 /usr/libexec/gcc/darwin/ppc/as -arch ppc -o op.o 

Here you can see that the user andy owns four instances of the bash shell. Within these shells, a vi session is active, ssh has connected to a server called "chimpy", and make is building an instance of Perl. make itself is calling elements of the GCC compiler suite, such as cc and as .

The numbers in the first column of the table show the PID number of each process. These are what you can feed to the commands listed in Table 3-2 in order to foreground, background, or send signals to them.

Alternatively, you can use shell-relative PIDs with these commands. Invoking jobs lists only those the processes running as children to the current shell:

 andy@honey[~/mosxnut3]$  jobs  [1]    running    sudo bin/safe_mysqld [2]  + vi README andy@honey[~/mosxnut3]$ 

The bracketed numbers leading each row of this output table can be used instead of PIDs when issuing process-control commands. The number can be prefaced with a percentage sign (%) to show that you're using a relative PID. So, to foreground that vi process, type fg %2 . In this particular instance, you can also type fg for the same effect; the plus-sign symbol next to the number says it's a child process, and hence the default target for commands like fg and bg .

For another view of a Terminal window's child processes, select File Get Info ( -I) and select the Processes choice from the resulting window's pop-up menu. See the section "The Terminal Inspector" earlier in this chapter, for more about this window's views and options.


To see a list of all the processes you're running on this machine, use ps x :

 andy@honey[~/mosxnut3]$  ps x  PID  TT  STAT      TIME COMMAND    97  ??  Ss     0:13.66 /System/Library/Frameworks/ApplicationServices    98  ??  Ss     0:03.03 /System/Library/CoreServices/loginwindow.app/   212  ??  Ss     0:00.52 /System/Library/CoreServices/pbs   218  ??  S      0:38.93 /System/Library/CoreServices/Dock.app/Contents/   220  ??  S      0:13.46 /System/Library/CoreServices/SystemUIServer.app/   222  ??  S      0:17.76 /System/Library/CoreServices/Finder.app/Contents/   225  ??  S      0:00.30 /Applications/iTunes.app/Contents/Resources/   227  ??  S      0:01.10 /Applications/iCal.app/Contents/Resources/   322  ??  S      0:00.69 /System/Library/Services/AppleSpell.service/   358  ??  S      0:00.47 /System/Library/CoreServices/RemoteManagement/   359  ??  S      0:00.04 /System/Library/CoreServices/RemoteManagement/   388  ??  S      0:34.33 /Applications/Quicksilver.app/Contents/MacOS/  1463  ??  S      0:02.15 /System/Library/CoreServices/Dock.app/Contents/  1469  ??  S      0:01.26 /System/Library/CoreServices/Dock.app/Contents/  1684  ??  S      0:32.87 /Applications/iChat.app/Contents/MacOS/iChat -psn_  1686  ??  S      4:06.34 /Applications/Firefox.app/Contents/MacOS/firefox-  1687  ??  S      1:30.28 /Applications/Utilities/Terminal.app/Contents/  1688  ??  Ss     0:02.53 /System/Library/Frameworks/InstantMessage.framewor  1691  ??  S      0:03.85 /Applications/iCal.app/Contents/MacOS/iCal -psn_0_  1693  ??  S      2:42.60 /Applications/X-Chat Aqua.app/Contents/MacOS/  3180  ??  S      4:53.76 /Applications/Microsoft Office 2004/Microsoft Word  3182  ??  S      0:04.51 /Applications/Microsoft Office 2004/Office/  3261  ??  S      0:00.90 /Applications/Stickies.app/Contents/MacOS/Stickies  3278  ??  S      3:09.86 /Applications/iTunes.app/Contents/MacOS/iTunes  3299  ??  SNs    0:02.06 /System/Library/Frameworks/CoreServices.framework/  3304  ??  S      0:14.04 /System/Library/CoreServices/Dock.app/Contents/  1692  p1  S      0:00.35 -bash  3273  p2  S      0:00.02 -bash  3284  p2  S+     0:00.76 ssh blitz  3311  p3  S      0:00.02 -bash  3313  p3  S+     0:00.05 vim outline.pod  3883  p4  S      0:00.03 -bash  4405  p4  S+     0:00.09 make perl  4443  p4  S+     0:00.00 /bin/sh -c 'sh  cflags "optimize='-pg'" toke.o'  4444  p4  S+     0:00.00 /bin/sh -c 'sh  cflags "optimize='-pg'" toke.o'  4445  p4  R+     0:00.03 sh cflags optimize='-pg' toke.o 

This lists both the Terminal-controlled programs and the Aqua applications that are running, as well as the frameworks, system services, and plug-ins used by those applications. They are, after all, just Unix programs, all with their own PIDs.

Running ps with the aux options lists every single process running on the machine, regardless of context or user. This would, at a typical moment in any Mac OS X machine's life, be enough to fill a couple of pages of this book. You can pipe this output through the grep command to automatically filter the results: ps ax grep bash shows a table describing all the bash processes every user is currently running, for example.

For a friendlier interface to browsing active processes, see the Activity Monitor ( /Applications/Utilities ).

3.2.1.1. Sending signals with kill and killall

As its name suggests, kill 's most common function involves terminating programs, through its default usage: kill pid . Actually, kill sends a Unix signal of some kind to the program, and the default happens to be the terminate ( TERM ) signal. You can send different signals through the kill - signal pid syntax, where signal is a signal name or number.

The even more violent-sounding killall is often more convenient than kill is. This really just lets you refer to processes by their name, saving you from having to look up their PIDs first. For example, killall tail sends the TERM signal to all tail processes running under one name.

killall -HUP process is a traditional Unix idiom for having a continually running process (such as a network daemon) reload its configuration information. However, if a Startup Script is available for this service, you should favor running that instead, even if they both ultimately have a similar effect; see the section "StartupItems" in Chapter 11.

3.2.2. Mac OS X's Console Mode

While not quite an alternative to using the Terminal, Mac OS X offers a console login mode that lets you boot into Darwin's command-line interface instead of Aqua.

To enter console mode, you first need to configure Mac OS X's login window so it only displays the name and passwords fields. To do this, launch System Preferences, and go to Accounts Login Options. In the Login Options pane, turn off automatic login, and set the "Display login window as option to "Name and password." Next, at the login window, identify yourself as >console and click Log In (or press Return) without providing a password; the login window gets replaced by a standard Unix login prompt. Just type in your username and password as directed, and you're in.

Once you log out (through the logout command or by exit ing your shell program), the Mac OS X's standard login window appears once more. You need to pull the >console trick again in order to reenter console mode; otherwise , subsequent logins will launch the Finder, as usual.



MAC OS X Tiger in a Nutshell
Mac OS X Tiger in a Nutshell: A Desktop Quick Reference (In a Nutshell (OReilly))
ISBN: 0596009437
EAN: 2147483647
Year: 2003
Pages: 130

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net