How can I run sub-minute cron jobs?

I would like to run a cron job every Wednesday at 10:31:10, but I just learned that crontab cannot run sub-minute jobs, so the closest I can get is 10:31 a.m. with the below code:

31 10 * * WED /file/to/run.py

Is it possible to hack around this, or are there other alternatives to cron that could do the job?

3 answers

  • answered 2018-02-13 00:51 TheWebs

    You can't. Cron has a 60 sec granularity.

    You can though build a SH script that sleeps for ten seconds and then does X, set your cron job to run a script at 10:31 AM on a wednesday that then sleeps for 10 seconds, then do x.

  • answered 2018-02-13 00:51 John3136

    Pass a number of seconds as a parameter and make your script sleep for that many seconds before doing it's thing.

    If you want it to run at 10:31:10 and 10:31:20 you'd have to see if it works to have 2 identical entries (with a different sleep value) else add some sort of repeat.

  • answered 2018-02-13 10:47 kvantour

    The easiest solution is to sleep for 10 seconds:

    # .----------------- minute (0 - 59)
    # |   .------------- hour (0 - 23)
    # |   |  .---------- day of month (1 - 31)
    # |   |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
    # |   |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)
    # |   |  |  |  |
    # *   *  *  *  *     command to be executed 
      31 10  *  *  3     sleep 10 && /file/to/run.py