LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How can I retrieve the next run time of a cron (https://www.linuxquestions.org/questions/linux-newbie-8/how-can-i-retrieve-the-next-run-time-of-a-cron-733143/)

vincentgagnoncoll 06-15-2009 03:26 PM

How can I retrieve the next run time of a cron
 
Hi guys,

I'm sure I am not the only one asking for this but I've been breaking my head to achieve this. Basicaly, I have made a cron like system in PHP using the same cron format as crontab.

My question is how can I get the next run time of of jobs just by executing a command.

Can you point me the right direction.

Thanks in advance.

MensaWater 06-15-2009 03:44 PM

You'd have to script something. Unlike "at", "cron" doesn't queue the job until run time. So there is no queue to examine (or more accurately - the crontab itself IS the queue).

You'd have to get the run specification list (e.g. by running "crontab -l <user>" and parse out the the data to determine every time something should run then check that against current time to determine which is the "next" time.

That is if you have an entry that says:
* * * * * ls -l /tmp/ls_l.out
You're telling it to run every minute. So the "next" time is the minute right after you execute your check.

However if you an entry that says:
00 05 * * 1-6 ls -l /tmp/ls_l.out
The "next" time will be the "next" time 5 AM comes after you run the check. If you run the check at 5:05 AM the "next" time is nearly 24 hours later but if you run it at 4:55 AM the "next" time is only 5 minutes away. Also since you've told it to do this only 6 days a week the "next" time might be nearly 48 hours away.

Such a script can be made but it would take some work.

To make it more interesting if you don't know which user runs it (or if multiple users run similar) you might need to get all the entries from /var/spool/cron/* and figure out which is "next".

vincentgagnoncoll 06-15-2009 03:55 PM

I understand all you have explained.

If at can know when to execute it, there must be a way to parse the cron format to know the exact time or am I completly out of track

MensaWater 06-15-2009 04:06 PM

When "at" runs it is putting in a queue entry for a single event.

Cron on the other hand allows you to automate running of a single event or (more commonly) multiple runs at the same time interval. So while it is easy to see the "at" queue it is a little more complicated with cron.

In fact you could make "at" more difficult simply by specifying the same command to run at multiple times (multiple "at" jobs). You'd again run into the need to figure out which of those times was the "next" time.


All times are GMT -5. The time now is 03:32 AM.