Jeff Abrahamson on Thu, 21 Feb 2002 10:50:12 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] shell newbie has question


On Wed, Feb 20, 2002 at 09:42:47PM -0500, Paul wrote:
> If I'm way off it's because I'm just throwing out a quick
> guess at what's going on here.  If you want something to run
> every minute, cron will do it.  Or, you could tell your
> shell script to "sleep 60" in the middle of a loop.
> 
> > Sounds like you want a cron job that runs like once a
> > minute, checks the directory for a file with
> > appropriate properties to see if it's new, then send
> > it off to be processed. Im sorta novice too so you
> > might want to waite for a couple more responses :-)

For something simple on a not very loaded station, a cron job is
simple and will do the job well, even if run once a minute. But
imagine starting to have several of these. Once a minute dozens of
little shell scripts all launch. At some point your machine starts to
feel some load.

If what you want is quite ad hoc, you can certainly just take an xterm
and type

    while true; do something; sleep 60; done &

and be very efficient. But if it ever dies, it's dead, and you won't
recover until you happen to check.


More robust, if you choose to bother, is to have something run by cron
occasionally. (I usually do once an hour as a trade-off between
potential down time and frequency of launching new things, but your
needs will dictate.) That something checks to see if your watcher is
running, and, if not, launches it.

Attached is a pair of perl scripts that do just this. The application
was that for a while my boyfriend didn't have access to a printer. So
he would print (on his Mac) to a non-existent LaserWriter 8 driver in
order to generate a postscript file. Then he would ftp it to a special
directory on my machine (a simple drag and drop for him). My folder
watcher once a minute would take ps files in that directory and print
them, removing the files afterwards.

(It was a kludge for him, but it was temporary. Although it was a
kludge that worked well enough that he delayed for months before
bothering to set up a working printer on his machine.)

Modifying this to do a task other than printing should be quite
straight-forward. Improving it shouldn't be hard, either, since it was
a ten minute hack with subsequent improvements as things failed here
or there.

-- 
 Jeff

 Jeff Abrahamson  <http://www.purple.com/jeff/>

 The Big Book of Misunderstanding, now in bookstores and on the web:
 <http://www.misunderstanding.net/buystuff.html>
#!/usr/local/bin/perl -w
#
# Start daemon if it's not already running.


use strict;
use FileHandle;


my $ps = `ps -auxw | grep lpwatchd | grep -v grep | grep -v start_lpwatchd`;
my $len= length($ps);
if($len == 0) {
    # First send me mail so I know this is happening.
    my $fh = new FileHandle;
    $fh->open('| /bin/mail -s "lpwatchd started" jeff@purple.com');
    print $fh "lpwatchd started";
    $fh->close;

    # Launch daemon. We won't return, but that's ok for now.
    my $home = $ENV{'HOME'};
    my $ret = `$home/bin/lpwatchd`;
    print "start_lpwatchd returned the following:\n$ret\n";
}

#!/usr/local/bin/perl -w
#
# Daemon to watch for new files in the print directory
# and, on finding any, print and rm them.

use strict;

my $home = $ENV{'HOME'};

while(1) {
    my @files = glob("$home/print/*");
    if(scalar(@files) > 0) {
	sleep 5;		# arbitrary, let ftp finish, should be enough for
				# our 10/100baseT network
	foreach my $f (@files) {
	    my $f1 = $f;
	    $f1 =~ s/ /_/g;
	    $f1 =~ tr/"'\\//d;
	    `$home/bin/jim_print "$f" $f1`;
	}
    }
    sleep 60;
}