1

I have a python script on my RaspberryPi that I want to run at these general times:

  • Weekdays, after 4:30 PM until 00:00 running every 30 minutes
  • Weekends, after 8:00 AM until 00:00, running every 60 minutes

I know how to schedule it daily with Cron,

30 16 * * 1-5 /mnt/myHDD/myscript.py

but can't figure out how to have it run every X minutes, until Y time.

I think to run every 30 minutes, I'd do this correct? But how to also run every hour from 16:30 until 02:00 next day?

1-59/30 * * 1-5 /mnt/myHdd/myscript.py

Edit: sorry, to clarify it doesn't have to be exactly every 30 minutes...the script simply downloads pictures from my phone to my rpi and I'd like it to run every so often during the weekdays when I'm home (ie after 1630/1700) and then on weekends every say hour from general "waking hours".

  • It's OK that the Friday night job runs until Saturday 2am, when your normal weekend runs are 8am-2am? (Ditto the Sunday job that goes into Monday morning). Also, could you correct the title/body disagreement on "every 20 minutes" vs "every 30 minutes"? – Jeff Schaller Apr 1 at 0:15
  • It's slightly difficult to do precisely what you ask with two cron table entries, because */30 will run every 30 minutes of matching hours, including at 1600 on weekdays. – DopeGhoti Apr 1 at 0:18
  • @JeffSchaller sorry about that, see edit. Basically if it goes until midnight and stops that's fine. Just wanting it to run every so often while home during weekdays and weekends. – BruceWayne Apr 1 at 0:40
  • Couldn't you just try and run it every hour? The script could check whether it's possible to sync, if not, just fail graciously. – Pierre B Apr 1 at 0:59
  • 1
    That would be 24 times per day, if the devices were running 24 hours per day. No, it's not a big deal. You could just run it 2x per hour. Just ask yourself how important this backups are to you. – Pierre B Apr 1 at 17:15
2

Please see my EDIT at the end of this answer.

  1. This answer is "close" to your first requirement:
*/30 16-23,0 * * 1-5

Translation: “At every 30th minute past every hour from 16 through 23 and 0 on every day-of-week from Monday through Friday.”

The first answer misses because it doesn't run every half-hour. Perhaps you could kludge around that by placing a sleep in your code:

time.sleep( 1800 )
  1. And this answer is "close" to your first requirement:
0,*/30 16-23,0 * * 1-5

Translation: “At minute 0 and every 30th minute past every hour from 16 through 23 and 0 on every day-of-week from Monday through Friday.”

The second answer misses because it runs at 16:00, 30 minutes in advance of when you wished to start. This is probably a "closer" match to your stated requirements, if that doesn't matter. If it does matter, and you can wait until 17:00, then a simple change to the schedule will take care of that:

0,*/30 17-23,0 * * 1-5
  1. This is the answer to your second requirement:
0 8-23,0 * * 6-7

EDIT: I had a mental block. It's occurred to me that there is an exact answer to your question. It's to have 3 crontab entries instead of just 2:

0,*/30 17-23,0 * * 1-5 /mnt/myHDD/myscript.py
30 16 * * 1-5 /mnt/myHDD/myscript.py
0 8-23,0 * * 6-7 /mnt/myHDD/myscript.py
  • 1
    FYI for future readers, I had to add python before the script, e.g. ... * * 1-5 python /mnt/myHDD/myscript.py. – BruceWayne Apr 18 at 21:56
  • 1
    @BruceWayne: Did you have a 'shebang' line at the top of your script? – Seamus Apr 18 at 22:49
  • 1
    Ah, good point - no I don't (you're referring to the #!/usr/bin/env python thing right?). I know know what the point of that is! Great point. – BruceWayne Apr 18 at 22:51
  • 1
    @BruceWayne: Yeah - that's it. Here's a good explanation for that & how to code the shebang for max portability. – Seamus Apr 18 at 22:57
2

I would set up two cron jobs:

# Mon-Fri every "half hour" from 16:30 until 23:30 and also at 12:30am and 12:59am
30,59 0,16-23 * * 1-5 /mnt/myHDD/myscript.py

# Weekends, every hour from 8am until 11pm
0 8-23 * * 0,6 /mnt/myHDD/myscript.py

The biggest approximation here is that I used minutes 30 & 59 for the weekday job, so that you didn't have one run at 16:00. As a result, the jobs are 29 and 31 minutes apart during the week. If that's a problem, split that entry into two:

# Mon-Fri at 16:30
30 16 * * 1-5 /mnt/myHDD/myscript.py

# Mon-Fri every half hour from 17:00 until 23:30 and also at midnight and 12:30am
0,30 0,17-23 * * 1-5 /mnt/myHDD/myscript.py

I hard-coded the minutes to be variously top-of-the-hour and 30-past, or 30-past and 59-past, but you could choose any interval (5 & 35, etc) if there are competing jobs.

The Mon-Fri entry will run at midnight and 12:30am; if you remove the 0 from the hour field, then the last run will be at 11:30pm. Again, if that's a problem, split out that entry to specifically run at midnight.

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.