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!


1 Answer 1


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. Commented Mar 26, 2018 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
    Commented Mar 26, 2018 at 11:37
  • Exactly, as soon as possible within the time range. Sorry for the lack of clarity. Commented Mar 27, 2018 at 11:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .