0

Main-agenda: I am trying to automate shutdown and restart of my computer using cron. To be exact, I want to shutdown my computer at 10:30 pm and restart my computer at 06:00 am everyday.

Progress so far:

  1. I can automate shutdown. (I don't even use cron. I just use sleep $duration; poweroff. And duration is computed as target time minus current time).

  2. For automating restart I am following this askubuntu answer. I could successfully run the "Simple test to wake the machine 5 minutes from now". It works fine!

  3. Now comes the part where I try to figure out: how to use cron. So I basically want to run something like this everyday:

    sudo sh -c "echo $(date +%s -d '+1 day 6:30') > /sys/class/rtc/rtc0/wakealarm"

    (Remark: standalone, the above works fine).

  4. So I try this: sudo crontab -e and added the following line there:

    */1 * * * * echo "$(date +%s -d '+1 day 6:30')" > /sys/class/rtc/rtc0/wakealarm

    Remark: I am using */1 * * * * for testing purposes only. Finally when everything works I plan to use @reboot.

  5. The above doesn't work. So I have tried looking online and debugging. From this unix.stackexchange answer I get the idea that "by default cron is using sh to run the task instead of bash". So I added SHELL=/bin/bash in the sudo crontab -e file.

  6. Next I have broken the above echo into simpler things and now, I know some of them work and some of them don't.

Currently, the output of sudo crontab -l is:

SHELL=/bin/bash
*/1 * * * * echo "$(date)" > /home/username/a.txt
*/1 * * * * echo "1659800001" > /sys/class/rtc/rtc0/wakealarm
*/1 * * * * echo "$(date +%s)" > /home/username/b.txt
*/1 * * * * echo "$(date +%s -d '+1 day 6:30')" > /sys/class/rtc/rtc0/wakealarm

Of these - the first 2 commands execute correctly! However the 3rd and 4th commands don't execute. 4th command is what I want to work.

So my guess is that $(date +%s) is the problem? Please help me proceed from here!

Inspired_Blue
  • 2,308
  • 3
  • 15
  • 21
  • 1
    If you're trying to send the time in seconds, why the `$`? Why not just `date +%s -d '+1 day 6:30' > /sys/class/rtc/rtc0/wakealarm`? I sense I'm missing something – Barry Carter Aug 06 '22 at 16:19
  • Thanks! The answer below also alluded to this. So I have responded to this in a comment to the answer. I still have a few questions. Please feel free to respond in that comment. Thanks again! – Inspired_Blue Aug 06 '22 at 17:13

1 Answers1

1

Your intuition about the % is correct. See man 5 crontab:

A "%" character in the command, unless escaped with a backslash (), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.

This will work with the % escaped:

*/1 * * * * echo "$(date +\%s -d '+1 day 6:30')" > /sys/class/rtc/rtc0/wakealarm

However, I wonder why you want to do the echo? This will also work:

*/1 * * * * date +\%s -d '+1 day 6:30' > /sys/class/rtc/rtc0/wakealarm
EricH
  • 26
  • 3
  • Yes that works. The reason I was trying `echo` was: because `sudo date +\%s -d '+1 day 6:30' > /sys/class/rtc/rtc0/wakealarm` would give: `bash: /sys/class/rtc/rtc0/wakealarm: Permission denied`. However `sudo sh -c "echo $(date +%s -d '+1 day 6:30') > /sys/class/rtc/rtc0/wakealarm"` was working. I also tried with `sh` but I got: `sh: 1: cannot create /sys/class/rtc/rtc0/wakealarm: Permission denied`. I wrongly concluded from this that `echo` must be necessary somehow. Just if you know: can you tell why the above wouldn't work in a `bash` prompt or in `sh` prompt? – Inspired_Blue Aug 06 '22 at 17:10