3 posts / 0 new
Last post
#1 Tue, 05/13/2008 - 22:23
SteveHeinsch

Cron Jobs

I am coding a custom mass email program for a client in php. I wrote a simple test cron job and got it working.

General cron question: what happens if a cron job is executed while a previous cron job is being executed?

This mass emailer so far has 2500 recipients. I was thinking of spacing them out by 1/4 second each as to try to avoid being labeled as spam by yahoo/gmail/msn etc. That works out to about 11 minutes execution time.

The design is simple. When the client makes an email to send out to a preexisting list, it copies that list into a separate table in mysql. The cron job checks this table for entries and if anything is in there, it will send the email and then delete that entry from the table. So lets say I set the cron job to execute every 10 minutes. If it takes 11 minutes to send out the list, what will happen since the previous cron job is still executing?

Any guidance appreciate...Im a cron n00b LOL. Thanks, Steve

Wed, 05/14/2008 - 00:46
Joe
Joe's picture

<div class='quote'>General cron question: what happens if a cron job is executed while a previous cron job is being executed?</div>

It executes. You're expecting cron to be quite a bit more advanced than it is. ;-) (There are quite advanced task scheduling systems out there--written by dudes who have a deep and abiding love for scheduling things--but cron is not one of them.)

If you don't want your application to step on previous instances, you have to check for previous instances yourself. Many Virtualmin and Webmin generated cronjobs do this by checking for a pid or a lock file. So, make yourself a file called &quot;running&quot; in the directory where your script lives when the process starts up--if it exists when it starts, exit and print out an error to that effect (or sleep for 5 minutes and try again--but make sure you eventually error out--only you know what a reasonable &quot;give up&quot; time would be). When the process is finished and is exiting, you remove that file.

When you do this, you have to become a bit more careful about catching errors rather than letting them cause your program to exit, since if you leave that lock file laying around after a failed run, the service will continue to fail until you clean it up manually.

--

Check out the forum guidelines!

Wed, 05/14/2008 - 10:06 (Reply to #2)
SteveHeinsch

Thanks Joe! Very helpful. I assumed thats what would happen if it was currently executing. What I did was create a lockfile as you suggested, and I inserted the current timestamp into it. If the timestamp is over an hour old, it deletes the lockfile to prevent the situation you described.

Topic locked