简体   繁体   中英

echo $(date +%s -d "+1 day 6:30") using sudo crontab -e [duplicate]

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文简体   中文繁体   中英对照 版本,有任何建议请联系yoyou2525@163.com。

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!

1 anwsers

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question
 
粤ICP备18138465号  © 2020-2022 STACKOOM.COM