Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Is there a command line tool that will simply evaluate a cron expression and return a bool response if it is supposed to be running right now? I'm looking for something I can use as a utility in another bash script. Something like so:

run_script=$(/tools/evaluate-cron-expression "02 4 * * *")
if [ "$run_script" -eq "1" ] # etc etc

I know, I know, I could just setup a real cron job, but I'm playing with the idea of wrapping all of my scheduled scripts inside of another script.

share|improve this question
1  
This is actually very cool and I could make it work, except my production servers are limited to PHP 5.2 :( github.com/mtdowling/cron-expression –  Jake A. Smith Nov 7 '12 at 21:21
    
What is the specific problem you are trying to solve? –  mdpc Nov 7 '12 at 21:29
    
I want to pass a cron expression to a script and, given the current time, determine whether or not it is a match. –  Jake A. Smith Nov 7 '12 at 21:33
    
I guess that is pretty specific, but again why? What more general problem are you trying to solve that would lead you to a solution like this? –  mdpc Nov 7 '12 at 21:35
1  
So you are basically duplicating the cron function? Why not use the functionality contained there....like putting the cron entries into seperate files under /etc/cron.d? –  mdpc Nov 7 '12 at 22:15

1 Answer 1

up vote 0 down vote accepted

You can find a Class that deals with this here: http://www.phpclasses.org/package/641-PHP-Compares-timestamps-with-crontab-style-specifiers.html

I've been using this Class for something like 7 years. It has worked flawlessly.

share|improve this answer
    
Thanks! Exactly what I wanted. –  Jake A. Smith Nov 10 '12 at 17:19

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.