Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a cronjob that runs php5 wp-cron.php every minute to update my website.

However something happened and i had 30+ instance of it (31 is marked on this one dump of ps aux). It ate up my ram, caused additional instance to terminate do to lack of memory and caused me not to be able to ssh into the box.

I can't understand why instances were living >30mins, one usually takes a few seconds. The day it happened i had no jobs planned (although maybe wp cache usesd it? but i never had a problem before)

What can i do to prevent a cronjob from spamming and destroying my memory? Is there a way i can say do not start if an instance is alive? and if an instance is alive for more then 5mins kill it?

Is there a way i can protect myself from something similar from happening?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

For the goal to prevent multiple copies from running, use flock (Linux), lockf (FreeBSD), shlock (provided with some systems, less reliable). This won't limit execution time but ensures only one process is running. Then, if it hangs, you can analyze its state on the fly.

You can limit CPU time of the spawned process using ulimit shell builtin.

To limit wall time, you can write script which waits for a process termination and kills it after timeout. It's easier in Python/Perl/etc. but shell also allows this (using trap and/on background children).

Sometimes it's useful to provide fixed time between invocations (i.e. from end of previous one to start of next one) instead of invocation starts, as cron does. Usual kinds of cron doesn't allow this, you should run special script.

share|improve this answer
add comment

I think the first thing you need to do is to intrigue the culprit which might have caused 31 instances of that job. Usually it could be your program being hanging at some point, you should debug and fix this problem unless you don't want your website updated successfully.

For "Is there a way i can say do not start if an instance is alive", yes, there are couple of ways, one of those is just check pgrep yourprogramname to see if an instance exist already, if yes, you can call pkill -x yourprogramname to kill them all.

share|improve this answer
    
wp-cron.php is wordpress cron which does things like publish post on a certain date/time. Its not my app (i dont touch php, i dislike it to much). I disabled system() and other commands so php cant wreak havoc. Is there some type of script i can use that takes in args to decide if it should launch or not? my bash and python i weak. +1 –  acidzombie24 Jul 14 '12 at 16:49
2  
Good answer, I'd like to add that pgrep is a simplier way to determine if a process is running, AND it won't report itself because of the command line being the same as the searched item. if [ -n "$(pgrep -f yourprogramname)" ] ; then exit ; fi pgrep only outputs PID's if it found something, otherwise the "" will be empty (thus the -n test) –  lornix Jul 14 '12 at 16:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.