I'm setting up a cron job to suspend and wake a server at certian times.

I'm using two commands. One to be run from mondays to thursdays:

rtcwake -m mem -t $(date +%s -d "08:00")

And another one to be run on fridays:

rtcwake -m mem -t $(date +%s -d "monday 08:00")

I wonder if it's possible to make this separation unnecessary. If date could understand a statement like next weekday 08:00, it'd simplify things a lot.

I wasn't able to find if this is actually possible or what syntax should I use.

I basically want to avoid my server waking up on saturdays and sundays.

  • Have you considered having two separate cron jobs? – Jeff Schaller Aug 9 '18 at 19:12

You may want to try

date +%s -d"$(( ( $(date +%w)==5 )?3:1 ))days 8:00"

EDIT: to answer your comment: date +%w yields the day of week, which then is evaluated by a "conditional operator" (expr?expr:expr): if expr1 evaluates to TRUE, evaluate and use expr2, else expr3. So, on Friday, we have day 5, and run date +%s -d"3days 8:00, on the other days date +%s -d"1days 8:00.

  • 1
    Note that in a cron job specification, the % character has to be escaped as \%. – Kusalananda Aug 9 '18 at 10:09
  • Hmm - yes, thanks - but not in his/her question above... – RudiC Aug 9 '18 at 10:13
  • "I'm setting up a cron job"... – Kusalananda Aug 9 '18 at 10:14
  • Thanks! Could you explain a bit how does this code work? – unfa Aug 9 '18 at 10:18

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.