Occasionally I find my computer tied up with some long-duration, resource-intensive application that one can't simply quit in the middle of. For example, iDVD can run for a couple days. Other times, on my family's multi-user machine, I'd like to be able to turn off other switched-out user's resource-hungry but idle apps (e.g. Word) that cause glitches when I'm doing something like using VLC Player or watching a QuickTime 480p movie.
Rather than quiting the app, I pause and later resume it by sending it Unix signals from the command line:
kill -s STOP 3328
The above command sends the STOP signal to, in this example, process 3328, which immediatly sleeps the process without aborting it. When I am ready to resume, I send it another signal to continue:
kill -s CONT 3328
Now you might be wondering why not simply use the commands nice and renice? Well two reasons. First, and primarily because, even at nice 19, sometimes that is not affirmative enough, espcially when the process is consuming resources other than the CPU: e.g. network intensive or disk intensive operations. Second, it's tricky to undo nicing and be sure you got it right.
This can be automated too: here's a real life motivation. I like to run long duration apps on the home computer while I'm at work. But I don't want those apps getting in the way of other famliy members who might want the computers full attention while I'm away. Since they are not Admins, and my login is switched out, my rude application is dominating the computer.
If for example, I'm remotely backing up the computer, their web browsing experience will be sluggish. Thus I run this one-line perl script (which you can create an alias for) that watches the computer. Any time it detects a load above some threshold level, it stops the long duration application. (Remember, this is one line; it's been broken for a narrower display. There's a space at the end of each line; be sure to copy that as well.)
perl -we 'die unless @ARGV;$SIG{INT} =sub {`kill -s CONT $p`; die
qq:bye now\n:};($p,$s,$c) = ( @ARGV,2.5,0.5);($sig,$w) =
(qq:CONT:,30);$c1=$c2=$e1=$e2=0;$e2=30;while (1) { last unless grep{
/^\s*$p /} `ps x`; ($x,$y,$z) = split /\s+/,`sysctl vm.loadavg`;
($sig,$w,$e1,$e2)=(qq:STOP:,300,300,0) if ($y>$s) ; ($sig,$w,$e1,$e2)
=(qq:CONT:,30,0,30) if ($z<$c);`kill -s $sig $p` ; print STDERR qq/ $p:
$x $y $z $sig S:$c1 C:$c2 sec /,chr(13); sleep
$w;$c1+=$e1;$c2+=$e2}'
So in this example, the three args on the command line are 3357, 2.3 and 0.5. In this case, 3357 is the process number of iDVD, 2.3 is the threshold load at which I want the process to stop, and 0.5 is the threshold load at which I want the process to resume.
Mac OS X Hints
http://hints.macworld.com/article.php?story=20060514081939278