6

It seems that Cron doesn't support a seconds interval. What is the easiest way to run a cli script (php) every 15 seconds? Is there a cron tool that works specifically with seconds (then I could use Cron to call it every minute)?

  • 2
    This is one for ServerFault isn't it? – David Neale May 11 '10 at 15:37
  • This question isn't mutually exclusive to SO, but I want to discover if there are any reliable progmatic solutions (ie, php cron libraries). – John Himmelman May 11 '10 at 16:19
2

You need to write a shell script like this that sleeps on the specified interval and schedule that to run every minute in cron:

#!/bin/sh
# Script: delay_cmd
sleep $1
shift
$*

Then schedule that to run in cron with your parameters: delay_cmd 15 mycommand parameters

5

I think you have to write a shell script which calls your command and then sleeps 15 seconds -- I don't believe most cron implementations allow for more than one minute granularity.

  • Is there a standalone command that adds minute granularity? There must be something more reliable, and community supported, than a shell script. – John Himmelman May 11 '10 at 16:17
3

Cron mandates the seconds to be there, as far as I knew.

"0/15 * * * * *"

sec min hour day-month month day-week (optional year which I left out)

EDIT: It appears that while the CRON expression requires 6 or 7, the cron exec that runs it wants 5... I'll look into this!

  • 2
    which implementation is this? manpagez.com/man/5/crontab en.wikipedia.org/wiki/Cron and "man" on my systems all show minute as the smallest unit. – Devin Ceartas May 11 '10 at 15:48
  • 1
    The cron tool apparently uses a subset of the "cron expression" (which is counter intuitive). I've used Quartz for Java based timing with success. en.wikipedia.org/wiki/CRON_expression However, I would certainly like to know how to do it with the cron utility - I was unaware the utility used something different. – glowcoder May 11 '10 at 15:54
  • Ironically, the cron wikipedia entry links to the cron_expression entry without noting that the expression allows for seconds but the utility doesn't. – glowcoder May 11 '10 at 15:55
  • Did you ever look more into this? – a CVn Nov 9 '16 at 13:51
  • "cron expression" is a dumb name for something that's incompatible with any actual cron implementation. Suggest those java people rename it. – user193597 Nov 9 '16 at 14:30
3

This is really a programming question and as such was asked on the correct site.

As cron itself cannot do what you require it's a matter of creating your own routine. The use of sleep, as suggested by others, will not give the correct results, as it does not take into account the time required to run the command. However, it's a simple matter of reading the system clock, in whatever language suits you, and firing off the command every 15 seconds based on the time, rather than a time delay.

Once you have your routine don't fire it off via cron every minute. Just run it as a background task continuously.

2
*/1 * * * * /path/to/script
*/1 * * * * sleep 15; /path/to/script
*/1 * * * * sleep 30; /path/to/script
*/1 * * * * sleep 45; /path/to/script
-2

0/15 * * * * ?

Cron format 0/15 * * * * ? Start time Wednesday, November 9, 2016 8:36 AM Next scheduled dates (Runs every 15 secs)

  1. Wednesday, November 9, 2016 8:36 AM
  2. Wednesday, November 9, 2016 8:36 AM
  3. Wednesday, November 9, 2016 8:37 AM
  4. Wednesday, November 9, 2016 8:37 AM
  5. Wednesday, November 9, 2016 8:37 AM

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.