Pause and resume resource-intensive apps via script

May 17, '06 07:28:00AM

Contributed by: SOX

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.

[robg adds: We covered stopping processes in this earlier hint. This hint, however, provides an automated solution based on processor load -- read on for the details. Note that I have not tested this one.]

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.

Every 30 seconds, the script checks the load average for the last minute, and if it exceeds the upper threshold, it STOPs the process. Then every 300 seconds, it checks the load average for the last ten minutes, and if it's below the lower threshold, it continues the process.

What the best numbers for detecting computer use by load monitoring are will depend upon how your computer is used, and the application in question. Some computers are very busy even when they are nominally inactive, because users have so many things running in the background. And some long duration applications have very high loads even when they are the only thing running. You can figure this out by monitoring the load with top. And to make this convenient, the above perl script also displays the loads for you, so you can see what happens.

Caveats: Not every application is going to take kindly to being suspended. Also suspended apps are truly frozen. You can't quit them, or move their open windows, or bring them to the foreground, until you unsuspend them. Also note that some applications, such as ffmpegX, actually run in multiple forks, so deciding which process number to freeze is not always straightforward.

Comments (11)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20060514081939278