2

I have a cron job that is running twice an hour. It runs once at HH:00 and once at HH:45. This is strange because I tried to specify that it should run every 45 minutes as follows:

*/45 * * * * python my_job.py

This works fine for other jobs I run every 5 minutes as well as jobs that run every 20 minutes. However, I wonder if the fact that an hour isn't evenly divisible by 45 minutes is causing strange behavior. Why would my cron job be running twice an hour with this setup?

4

Your job runs on every minute that is a multiple of 45, i.e. whenever minute % 45 == 0. So it will run at hh:00 and hh:45.

If it were an exact factor of 60, it would run at even-sized intervals.

To make it run at 45-minute intervals, I think you will need three rules, one for each hour (mod 3).

Although I haven't tried it, I believe the following will work:

0,45 0-23/3 * * * /usr/local/bin/myjob
30   1-23/3 * * * /usr/local/bin/myjob
15   2-23/3 * * * /usr/local/bin/myjob
1

The gross way to do this via cron would be:

    0,45 0,3,6,9,12,15,18,21  * * *
    30   1,4,7,10,13,16,19,22 * * *
    15   2,5,8,11,14,17,20,23 * * * .

A more elegant way to do this would be modifying your script to that it is time-aware and checks itself against a cron entry running every 15 minutes:

    */15 * * * * ... . 

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.