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.

Is there any way to access the cron timers and get the seconds left until the next execution of scripts in the crontab or maybe the seconds since the last?

share|improve this question
1  
That'll depend on what cron package you're using. Some might have that feature, but I don't think many do. –  Mat Feb 27 '13 at 6:28
    
Neither standard Vixie cron nor most current analogs like anacron can this. But evolution doesn't stop and, e.g., systemd invents its own cron. Did you analyze it? –  Netch Feb 27 '13 at 7:37
1  
Depending on your cron's logging capabilities and settings, you can get the time of last run from the logs. For example mine logs into /var/log/cron with info level, producing lines like “Feb 27 01:10:01 manatwork crond[468]: FILE /var/spool/cron/crontabs/manatwork USER manatwork PID 3286 /home/manatwork/cleanup.sh”. Calculating the seconds since the run is up to you. –  manatwork Feb 27 '13 at 8:03
add comment

1 Answer

Not easily. crond waits for a signal from the kernel and goes to sleep. When it gets the signal it checks whether there is any cronjobs to execute in that minute, launches those, and goes back to sleep.

It is a very efficient design - cron doesn't use any CPU while it is sleeping. It doesn't have an awareness of time passing either. As it goes to sleep it sets a "timeout" based on how long it is before the next command from any "registered" cronjob must run.

On Solaris 10:

sol10-primary:/> # pflags  271  
271:    /usr/sbin/cron
        data model = _ILP32  flags = ORPHAN|MSACCT|MSFORK
 /1:    flags = ASLEEP  pollsys(0x8047c70,0x1,0x8047ce8,0x806ba00)

While tracing the cron daemon, you will see it goes to sleep with a timeout, as below:

waitid(P_ALL, 0, 0x08047CD0, WEXITED|WTRAPPED|WNOHANG) Err#10 ECHILD
time()                                          = 1361952435
pollsys(0x08047C70, 1, 0x08047CE8, 0x0806BA00) (sleeping...)
        fd=3  ev=POLLRDNORM rev=0
        timeout: 105.000000000 sec
        sigmask = 0 0 0 0

When you update any cron job, the sleeping process is also woken up but to update its own configuration, after which it will go back to sleep with the new timeout value.

It is possible to see the timeout that was set. Notice that cron called the time syscall just before when it went to sleep (which returns the seconds since the epoc), so if you have observed this (i.e. traced the process when it called the time() syscall, you will be able to subtract that time from current and compare it to the timeout set on the pollsys call.

So... as I said, not easily.

share|improve this answer
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.