TweetFollow Us on Twitter

Process Control

Volume Number: 18 (2002)
Issue Number: 10
Column Tag: Section 7

Process Control

ps, top, kill, ...

by Rich Morin

A recent foray into Internet Radio conflicted in some manner with the OSX screen saver on my desktop machine. As a result, I was unable to get the screen saver to go away. I tried all of the usual tricks: hitting mouse buttons, moving the mouse, advancing to keyboard keys (e.g., shift, return) and finally "magic key combinations" (e.g., command-option-escape). No luck.

On Mac OS 9, my next steps would have been to try a "three-finger salute" (i.e., command-control-power), followed by pushing the reset button and if need be, pulling the plug. This being a BSD-based machine, however, I had a better solution at hand.

I went to another machine on the local net, logged into my desktop machine, and killed a couple of sorely-confused processes. I then logged out and returned to my desk, flushed with victory and the knowledge that I had a useful Section 7 topic in hand.

Advance Preparation

My "remote login" trick wouldn't have worked without a bit of advance preparation. OSX, for very good reasons, turns off most "remote services", by default. I don't recommend turning services on, willy-nilly, but ftp and ssh are so handy that I wouldn't want to live without them. And, because my desktop machine is hidden behind a firewall, I consider the risk to be pretty minimal (your mileage may vary).

In any case, here's how to allow remote ftp and ssh on an OSX 10.2 machine: In the System Preferences application, select the Sharing dialog. Under the "Services" tab, turn on the checkboxes labelled "FTP Access" and "Rmote Login".

You will also need a way to "find" your machine from the other one. This can be achieved in any number of ways, including DNS (Domain Name System), /etc/hosts files, etc. Your network administrator will (or should :-) know how to make this happen on your local network. As a last resort, you can use your machine's IP (Internet Protocol) address as a name.

Logging In

Because ftp and ssh aren't OSX-specific tools, you can log in remotely from almost any operating system. This being an OSX-related column, however, I'll assume that you're running OSX. If you're not, you'll have to deal with the vagaries of installing and starting up the needed clients on the machine you're on. On OSX, just start up a Terminal window and type:

% ssh <me>@<mydesk>
<me>@<mydesk>s password:

The first time you do this from a given machine, you'll be asked whether you trust the SSH keys on your desktop machine. Say yes (:-). After entering your password, you'll get a prompt from your desktop machine:

<me>@<mydesk> [~] 1: 

There are all sorts of things you can do at this point, but try not to get too excited and do something you'll regret. Think "delicate surgery", rather than "hack and slash". Thus, the first thing we want to do is find out a bit about the programs that are running over on the desktop machine. To do this, we'll use the top command, which displays system usage statistics and then lists the top processes, in terms of CPU utilization:

<me>@<mydesk> [~] 2: top
Processes:  65 total, 3 running, 62 sleeping... 190 threads   17:39:16
Load Avg:   2.15, 1.46, 1.35
CPU usage:  62.3% user, 37.7% sys, 0.0% idle
SharedLibs: num =  116, resident = 23.2M code, ...
MemRegions: num = 4936, resident =  159M + 13.0M private, ...
VM:         4.22G + 45.8M  5363(0) pageins, 0(0) pageouts   
   PID COMMAND            %CPU      TIME         ...
   758 top                7.9%      0:38.79   ...
   703 tcsh               0.0%      0:00.34   ...
   702 sshd               0.0%      0:00.78   ...
   701 ssh                0.0%      0:00.86   ...
   692 CCacheServ         0.0%      0:06.09   ...
   610 Finder             0.0%      0:18.75   ... 
   ...

Unlike most BSD commands, top doesn't just run and go away. Instead, it refreshes its display about once a second, letting you see if things are changing. When you're done viewing the display, type "q" (or Conrtol-C) and the program will terminate. Alternatively, just start up another Terminal window, leaving top running in the first one...

The system usage statistics are a bit arcane; look at top's man page if you want the details. For now, just note that the number of processes should normally be under 100 and that the load average (time-averaged system load) should be under five. If you see higher values on your machine, you may have a runaway application.

Because top's process list is sorted by CPU utilization, active programs tend to "bubble up". So, look near the top of the list for suspects. In some cases, killing off a sijngle process won't be enough; to restore my desktop to proper functionality, I had to kill the screen saver, the Internet Radio application, and the Finder!

I issued a separate kill com mand for each process I wanted to terminate, specifying the PID (process ID). This let me see and evaluate the effect of each action:

^C<me>@<mydesk> [~] 3: kill -9 610

Be sure to get the PID right; you don't want to kill off an innocent application, by mistake! By the way, kill can do more than just terminate processes. It can send any desired "signal" to one or more specified processes. So, for example, it can be used to tell processes to check their configuration files, etc.

If you can't figure out which command you need to kill, you may need to get a different view of the information. The ps command lists "process status" information, in a variety of formats. For our present purposes, "ps awx" is a reasonable idiom:

<me>@<mydesk> [~] 4: ps awx
  PID  TT  STAT      TIME COMMAND
    1  ??  SLs    0:00.03 /sbin/init     
    2  ??  SL     0:02.35 /sbin/mach_init        
   41  ??  Ss     0:02.24 kextd
  ...

To list the commands in a different order, just pipe together some commands:

<me>@<mydesk> [~] 5: ps awx | sort +3 -rn | more
   62  ??  Ss    56:09.79 .../WindowServer
  ...
  PID  TT  STAT      TIME COMMAND
  994 std  R+     0:00.00 more
  993 std  R+     0:00.01 sort +3 -rn
  ...
  610  ??  S      0:18.80 .../MacOS/Finder -psn_0_3538945
  ...

Transliterated, the pipeline above says to run the output of ps through a line-oriented sort (sorting by the fourth column, in reverse numeric order), then pipe the result through more (a text viewing command). If you know what command you're looking for, of course, you can use grep:

<me>@<mydesk> [~] 6: ps awx | grep Finder
 1006 std  R+     0:00.00 grep Finder
  610  ??  S      0:18.80 .../Finder -psn_0_3538945

With the advent of OSX 10.2, I feel more comfortable in recommending FreeBSD-related books to OSX users and programmers. There are still some differences, to be sure, but any book that covers FreeBSD 4.X commands will be a good match for the OSX 10.2 command set.

Although processes are the workhorses of any BSD system, there are few books that spend much time on them. Quite a few man pages relate to processes, however, and the DOSSIER (www.ptf.com/dossier) volume "Processes: FreeBSD" brings them together in a single volume.


Rich Morin has been using computers since 1970, Unix since 1983, and Mac-based Unix since 1986 (when he helped Apple create A/UX 1.0). When he isn't writing this column, Rich runs Prime Time Freeware (www.ptf.com), a publisher of books and CD-ROMs for the Free and Open Source software community. Feel free to write to Rich at rdm@ptf.com.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Recruit two powerful-sounding students t...
I am a fan of anime, and I hear about a lot that comes through, but one that escaped my attention until now is A Certain Scientific Railgun T, and that name is very enticing. If it's new to you too, then players of Blue Archive can get a hands-on... | Read more »
Top Hat Studios unveils a new gameplay t...
There are a lot of big games coming that you might be excited about, but one of those I am most interested in is Athenian Rhapsody because it looks delightfully silly. The developers behind this project, the rather fancy-sounding Top Hat Studios,... | Read more »
Bound through time on the hunt for sneak...
Have you ever sat down and wondered what would happen if Dr Who and Sherlock Holmes went on an adventure? Well, besides probably being the best mash-up of English fiction, you'd get the Hidden Through Time series, and now Rogueside has announced... | Read more »
The secrets of Penacony might soon come...
Version 2.2 of Honkai: Star Rail is on the horizon and brings the culmination of the Penacony adventure after quite the escalation in the latest story quests. To help you through this new expansion is the introduction of two powerful new... | Read more »
The Legend of Heroes: Trails of Cold Ste...
I adore game series that have connecting lore and stories, which of course means the Legend of Heroes is very dear to me, Trails lore has been building for two decades. Excitedly, the next stage is upon us as Userjoy has announced the upcoming... | Read more »
Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »

Price Scanner via MacPrices.net

May 2024 Apple Education discounts on MacBook...
If you’re a student, teacher, or staff member at any educational institution, you can use your .edu email address when ordering at Apple Education to take up to $300 off the purchase of a new MacBook... Read more
Clearance 16-inch M2 Pro MacBook Pros in stoc...
Apple has clearance 16″ M2 Pro MacBook Pros available in their Certified Refurbished store starting at $2049 and ranging up to $450 off original MSRP. Each model features a new outer case, shipping... Read more
Save $300 at Apple on 14-inch M3 MacBook Pros...
Apple has 14″ M3 MacBook Pros with 16GB of RAM, Certified Refurbished, available for $270-$300 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year warranty is... Read more
Apple continues to offer 14-inch M3 MacBook P...
Apple has 14″ M3 MacBook Pros, Certified Refurbished, available starting at only $1359 and ranging up to $270 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year... Read more
Apple AirPods Pro with USB-C return to all-ti...
Amazon has Apple’s AirPods Pro with USB-C in stock and on sale for $179.99 including free shipping. Their price is $70 (28%) off MSRP, and it’s currently the lowest price available for new AirPods... Read more
Apple Magic Keyboards for iPads are on sale f...
Amazon has Apple Magic Keyboards for iPads on sale today for up to $70 off MSRP, shipping included: – Magic Keyboard for 10th-generation Apple iPad: $199, save $50 – Magic Keyboard for 11″ iPad Pro/... Read more
Apple’s 13-inch M2 MacBook Airs return to rec...
Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices currently... Read more
Best Buy is clearing out iPad Airs for up to...
In advance of next week’s probably release of new and updated iPad Airs, Best Buy has 10.9″ M1 WiFi iPad Airs on record-low sale prices for up to $200 off Apple’s MSRP, starting at $399. Sale prices... Read more
Every version of Apple Pencil is on sale toda...
Best Buy has all Apple Pencils on sale today for $79, ranging up to 39% off MSRP for some models. Sale prices for online orders only, in-store prices may vary. Order online and choose free shipping... Read more
Sunday Sale: Apple Studio Display with Standa...
Amazon has the standard-glass Apple Studio Display on sale for $300 off MSRP for a limited time. Shipping is free: – Studio Display (Standard glass): $1299.97 $300 off MSRP For the latest prices and... Read more

Jobs Board

Liquor Stock Clerk - S. *Apple* St. - Idaho...
Liquor Stock Clerk - S. Apple St. Boise Posting Begin Date: 2023/10/10 Posting End Date: 2024/10/14 Category: Retail Sub Category: Customer Service Work Type: Part Read more
*Apple* App Developer - Datrose (United Stat...
…year experiencein programming and have computer knowledge with SWIFT. Job Responsibilites: Apple App Developer is expected to support essential tasks for the RxASL Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.