Working with Processes

The ps command (short for process status) outputs information about the various processes on your system. However, if you just execute ps by itself on the command line, you’ll see only the process information about the shell process you are running. For information about all the processes that belong to you, use the ps -x command, as shown in Example 12-14.

Example 12-14. Listing all the processes that belong to a user

$ ps -x
  PID  TT  STAT      TIME COMMAND
   71  ??  Ss     0:23.61 /System/Library/Frameworks/ApplicationServices.framework/
   72  ??  Ss     0:13.62 /System/Library/CoreServices/loginwindow.app/Contents/Mac
  131  ??  Ss     0:00.44 /System/Library/CoreServices/pbs
  138  ??  S      0:42.75 /System/Library/CoreServices/Dock.app/Contents/MacOS/Dock
  139  ??  S      0:40.96 /System/Library/CoreServices/SystemUIServer.app/Contents/
  145  ??  Ss     0:36.46 /System/Library/CoreServices/MirrorAgent.app/Contents/Mac
  147  ??  Ss     0:00.36 /System/Library/PrivateFrameworks/DMNotification.framewor
  157  ??  S      0:53.75 /Users/jldera/Library/PreferencePanes/Growl.prefPane/Cont
  159  ??  S      0:00.55 /Applications/iTunes.app/Contents/Resources/iTunesHelper.
  160  ??  S      0:01.02 /Applications/iCal.app/Contents/Resources/iCalAlarmSchedu
  161  ??  S      0:32.99 /Applications/iCal.app/Contents/MacOS/iCal -psn_0_1572865

When combined with grep, you’ll find that ps is just the process you are looking for. Example 12-15 shows a command to return the process information for Safari.

Example 12-15. Looking for Safari using ps and grep

$ ps -x | grep Safari
1758  ??  S     23:46.64 /Applications/Safari.app/Contents/MacOS/Safari -psn_0_17301505
 422  p2  R+     0:00.00 grep Safari

To see all the processes running on the system, use the -ax option. This produces quite a bit of content, so it’s best to use this option with grep to narrow down the results to only what you want to see. For example, if you want to see all the processes running as root, you would use:

    $ ps -aux | grep root

Another useful command for monitoring your system is top . In fact, it’s almost certain that top provided the inspiration for the Activity Monitor’s process view. When you execute top, your Terminal window fills up with a list of processes as well as the percentage of processor time and memory they are using, as shown in Figure 12-8. When you are done with top, use Control-C to quit, or use the Mac standard Looking for Safari using ps and grep-. command.

Tip

Invariably, top displays too much information in a standard Terminal window. One helpful hint is that you might want to resize the Terminal window before you run top so you can see everything it has to offer.

Killing Processes

Unfortunately, there are times when you have to manually intervene and end the execution of a process. Maybe it is a buggy program that has stopped accepting user input (like when you see the “Spinning Beach Ball of Death” when an application freezes), or maybe the program is just consuming a huge dataset and you’ve decided

top running in a Terminal window

Figure 12-8. top running in a Terminal window

you can’t wait for it to finish the task. Or as an administrator, you might need to log into a remote system to kill a user’s errant process or even reboot a server. Whatever the reason, here are the ways in which you can stop a process’s execution.

Force Quit

The easiest way to kill off an application while logged in to the system is with the Force Quit Applications window , shown in Figure 12-9. You can open this window by choosing the Force Quit Force Quit menu item or using the Option-Force Quit-Escape keystroke. The Force Quit window contains a list of all active GUI applications, with hung applications appearing in red (sometimes with the words “Not Responding” next to them in parentheses). Simply select the application you want to kill and click the Force Quit button.

Tip

Only GUI applications show up in the Force Quit window. To quit non-GUI applications, you’ll need to use the Activity Monitor, as described in the next section, or the Unix kill command.

You can also Force Quit an application using its icon on the Dock. If you Control-Option-click an application’s icon in the Dock (or click and hold the mouse button down for a second or two), a contextual menu pops up. Select Force Quit from that contextual menu, and the application that’s causing you problems quits instantaneously.

The Force Quit Applications window

Figure 12-9. The Force Quit Applications window

Using the Activity Monitor

The Activity Monitor also provides a way to force applications to quit, including those processes that aren’t visible on the display. To quit an application this way, simply highlight the process and then use the Quit Process button or the View Quit Process menu item (Option-Using the Activity Monitor-Q). This brings up a dialog box allowing you to Quit or Force Quit an application.

Using the command line

As always, there is a command-line tool waiting for you if you can’t use the GUI tools. Appropriately enough, it’s named kill . To kill a process, you need to first get its process ID via the ps tool. Then you just pass that process ID to kill, as shown in Example 12-16.

Example 12-16. Using the kill tool

$ ps -x | grep Safari
 1758  ??  S     24:38.16 /Applications/Safari.app/Contents/MacOS/Safari -psn_0_173
  468  p2  R+     0:00.00 grep Safari
$ kill 1758

If a process is being unruly, you can tell the system to kill it without prejudice by using the -KILL option. Example 12-17 shows this in action.

Example 12-17. Using kill -KILL to terminate a process without prejudice

$ ps -x | grep iTunes
 1732  ??  S     17:44.61 /Applications/iTunes.app/Contents/MacOS/iTunes -psn_0_170
  498  p2  R+     0:00.00 grep iTunes
$ kill -KILL 1732

Warning

You should exercise great care when killing errant processes, especially when using the kill command, as it’s easy to create problems when doing so. For example, every system administrator has a story about executing sudo kill -KILL 1 instead of sudo kill -KILL 128. Unfortunately, the launchd process has the ID of 1, which causes this command to hang the system.

kill has another commonly used switch, -HUP. When you kill a process using the -HUP option, the process reloads its configuration files and reinitializes itself. Not all processes will correctly interpret this command, however. It’s mostly used for reinitializing Unix daemons like named or httpd.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.184.6