Friday, October 1, 2021

Wake for Cron

I recently installed an SSD in my computer and decided to upgrade to Slackware -current (the developmental branch, soon to be Slackware 15). One pleasant surprise is that for the first time my computer will sleep and wake properly. That's great, but that means the various cron jobs won't run because my computer will be asleep when they're supposed to run. I thought of using anacron since I had used that many moons ago when I was running Red Hat. My concern was that it hadn't been updated since June 2000.

So I did a little research and decided to use rtcwake. That will make the computer wake up at a time you set. I checked /usr/spool/cron/crontabs/root to see when the cron jobs run. Turns out that daily runs at 4:40 A.M., weekly at 4:30, and monthly at 4:20. I knew that when my computer woke up, KDE would automatically put it back to sleep after an hour. So I decided to wake it up at 4:10 A.M. Everything seemed to work fine. So I created another file in cron.daily called rtcwake with the following in it:

#!/bin/sh
/usr/sbin/rtcwake -m no -l -t $(date +\%s -d "tomorrow 04:10")

The -m no tells it not to immediately put the device to sleep. -l means that the clock is set to local time and not UTC. The crazy looking date command translates "tomorrow at 4:10 A.M." into the time since the Unix epoch (number of seconds since midnight on January 1, 1970) which the rtcwake command command needs. I executed the command on its own last night. It ran properly. That cron job makes sure it will run tomorrow too. Pretty handy!