如何设置在 cron 计划中使用本地时间?

Sha*_*mat 9 cron

Cron 默认使用 UTC 时区。如何将其设置为在 cron 表达式(用于 cron 计划)中使用当地时间(例如 CST)。

rnd*_*Guy 12

您可以将系统的时区设置为预期时区,然后在 cron 作业中指定该时区的时间:

sudo timedatectl set-timezone America/New_York
Run Code Online (Sandbox Code Playgroud)

并确认输入timedatectl,或者执行

sudo dpkg-reconfigure tzdata
Run Code Online (Sandbox Code Playgroud)

更改时区后,请确保重新启动 cron:

sudo service cron restart
Run Code Online (Sandbox Code Playgroud)

计划任务:

30 5 * * * echo "run at half past 5" >> ~/logfile.log 2>&1
Run Code Online (Sandbox Code Playgroud)


Ric*_*chG -3

Cron 不关心时区。它将当前时间与 cron 字符串进行比较,如果匹配则执行作业。

  • 不!那不是真的。 (3认同)