in OPS

Linux system online only on working hours

E.g. you have a linux machine and want it online only MO-FR 07:00-19:00?

As root do the following:

1. create a new script (I used /opt/shutdownWakeUpNextWorkday7.sh)

#!/bin/bash
TOMORROW=$(date --date="tomorrow" +%A)
if [ $TOMORROW = "Saturday" ]
then
  TOMORROW="Monday"
fi
rtcwake -m off -t $(date --date="$TOMORROW 7" +%s)

2. make that script executable

chmod +x /opt/shutdownWakeUpNextWorkday7.sh

3. in /etc/crontab add

00 19   * * *   root    /opt/shutdownWakeUpNextWorkday7.sh

Done!

Notes:

The script checks whether the next day is Saturday, if yes it configures the rtcwake command to wake on Monday instead (skipping Sunday). On my systems computer time equals local time. If you have a different setup (rtc set to UTC) there are paramteters for both date and rtcwake to use either the one or the other so that your dates align.

rtcwake is used with powerstate off which equals ACPI S5, apparently some very old systems don’t support that. If you happen to be in that situation use something else like standby (S1), mem (S3) or disk (S4) and comment below I’d be interested to know.