I need to run a script between some hours of a given day of week as soon as the machine is turned on. For example: every Monday between 07:00 and 10:00.

I know how to handle the time conditions "between some hours of a given day of week" and "as soon as the machine is turned on" independently with cron jobs. However, I did not find so far an elegant solution to combine both.

I explore the option to run at the end of the time range (but not what I want).

I also saw the option of Anacron: in this question previously asked but the granularity of anacron is not fine enough.

How could I implement such pseudo-periodic? Sould I combine a cron job @reboot plus a script?

Thank you!


It sounds like you'd want a @reboot job.

The script could then check for the day of the week and time of day:


day=$( date +%u ) # day of week as integer, 1 == Monday
tm=$( date +$k )  # time of day, 0-23

if [ "$day" -ne 1 ] || [ "$tm" -lt 7 ] || [ "$tm" -gt 10 ]; then
    # wrong day and/or time

# do stuff here
  • Thanks for the answer! But I guess a sleep/wait condition is missing. If the reboot happens before the minimum hour, it should wait to be sure the script is executed once. – Uncle Ben Ben Mar 26 '18 at 11:34
  • @UncleBenBen Ah, if the machine is turned on outside of the time interval, and stays on, you want the job to run as soon as the time interval starts? This was not clear from the question. – Kusalananda Mar 26 '18 at 11:37
  • Exactly, as soon as possible within the time range. Sorry for the lack of clarity. – Uncle Ben Ben Mar 27 '18 at 11:45

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.