[lug] Automatic removal of cron job by cron script

Michael J. Hammel mjhammel at graphics-muse.org
Fri Nov 2 17:24:19 MDT 2007


On Fri, 2007-11-02 at 16:10 -0700, karl horlen wrote:
> I've created a cron job script called 'monitor' that
> monitors the health of a hardware component.  When an
> error is detected, I want this script to:
> 
> 1) Call another script called 'alert' to send a
> repeating error message (using an endless loop with
> sleep 2) to the console as an alert for staff.  
> 2) Remove the original cron JOB (remove the entry from
> the crontab file from within the original cron script
> 'monitor') versus just killing the actual running
> script 'monitor'.  If I don't remove the job, it will
> keep spawning more 'monitor' processes and I will have
> only killed the last process.

I would think you'd want to have the alert send messages for as long as
the error is detected, up until the error is cleared.  This would simply
be a matter of using a single script with multiple modes.  The monitor
script always monitors (re: always runs under cron) and each time an
error is found it sends the message.  It would continue to run as a cron
job.  When the error is cleared it would stop sending alert messages.

Alternatively, if you really want the monitor to not run and just let
the alert keep running, then just put a config file that the monitor
script updates before it starts the alert script.  If it sees the config
file (it exists at all) then it doesn't do anything.  Cron keeps calling
monitor, but because the config file is there it doesn't do anything.
If you do launch alert and have it run indefinitely then you'll want to
exec() it so monitor can exit and cron can complete that particular
cycle.  That will leave alert running as a child of init, however.

These two methods are easier than trying to modify the cron config from
within a running cron job.

-- 
Michael J. Hammel                                    Senior Software Engineer
mjhammel at graphics-muse.org                           http://graphics-muse.org
------------------------------------------------------------------------------
"In the republic of mediocrity, genius is dangerous."
Robert S.  Ingersoll, American lawyer and politician (1833-1899).




More information about the LUG mailing list