LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-15-2009, 04:26 PM   #1
vincentgagnoncoll
LQ Newbie
 
Registered: Jun 2009
Posts: 2

Rep: Reputation: 0
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.
 
Old 06-15-2009, 04:44 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,053
Blog Entries: 14

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
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".
 
Old 06-15-2009, 04:55 PM   #3
vincentgagnoncoll
LQ Newbie
 
Registered: Jun 2009
Posts: 2

Original Poster
Rep: Reputation: 0
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
 
Old 06-15-2009, 05:06 PM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,053
Blog Entries: 14

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is a script, run at boot time from init.d, run with root authority? tmbrwolf53 Linux - Server 2 03-31-2007 09:15 PM
change cron.daily run time jimmyjiang Red Hat 4 03-02-2007 02:35 PM
Thunderbird won't retrieve email - time out error! Adrian Baker Linux - Software 2 11-27-2006 02:51 PM
Howto run a job on a fixed time with cron? Schmurff Linux - Newbie 1 04-02-2004 04:31 AM
How to retrieve email from cron scripts ? wallywood Linux - General 1 03-05-2004 04:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration