#!/n/boston/v/dreeves/bin/mash
(* Runs a program at random times. *)

Get[Environment["HOME"]<>"/lib/Util.m"];
SetDirectory[Environment["HOME"]<>"/www/improvetheworld/mboxparser"];

program = "./monitor.pl ../archives.txt";
processIdFile = "RUNNING";
sleepMin = 10^8;
sleepMax = 10^8;  

(* purge items in processIdFile that aren't actually running anymore *)
Run[Environment["HOME"]<>"/bin/signal-all.pl "<>processIdFile<>" ALRM"];
fi = FileInformation[processIdFile];
If[!(fi==={} || (ByteCount/.fi) == 0), 
  pout["Launcher already running!\n"]; 
  Exit[];
];
(* ok, no other instances seem to be running, so... *)
registerProcess["RUNNING", strout["(", intersperse[" ", ARGV], ")"]];

While[True,
  Run[program];
  Pause[Random[Integer, {sleepMin, sleepMax}]];
];

