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?
|
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:
While tracing the cron daemon, you will see it goes to sleep with a timeout, as below:
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. |
|||
|
|
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 at 8:03