
我在 cronjob 中設定了一個腳本,在 00 點運行,但它提前 5 小時運行,即當天當地晚上 7 點運行。我的意思是它應該在當地時間 00 點運行,但它在 UTC/GMT 00 點運行。
timedatectl output
===================
Local time: Wed 2020-12-02 01:33:31 EST
Universal time: Wed 2020-12-02 06:33:31 UTC
RTC time: Wed 2020-12-02 06:33:31
Time zone: America/Toronto (EST, -0500)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: no
Last DST change: DST ended at
Sun 2020-11-01 01:59:59 EDT
Sun 2020-11-01 01:00:00 EST
Next DST change: DST begins (the clock jumps one hour forward) at
Sun 2021-03-14 01:59:59 EST
Sun 2021-03-14 03:00:00 EDT
ls -lrt /etc/localtime output
================================
lrwxrwxrwx. 1 root root 35 Nov 24 11:43 /etc/localtime -> /usr/share/zoneinfo/America/Toronto
date output
============
Wed Dec 2 01:42:28 EST 2020
我的系統上沒有任何名為 /etc/default/cron 的檔案。但CRON 守護程式重啟後,CRON 依照本地時間運行。所以問題是在重新啟動之前,即在系統實例化/製作之後,cron 需要 UTC 時間,但是當 cron 在任何節點上重新啟動時,它都會以本地時間正常運行。在 /etc/localtime 的符號連結設定為 /usr/share/zoneinfo/America/Toronto 之前,是否已經以預設時區 (UTC) 啟動了 cron?如果是,我如何在 cron 啟動之前更改時區?
hostnamectl output
==================
Static hostname: abcxyz-0
Icon name: computer-vm
Chassis: vm
Machine ID: 88d24fe7c825442dbfd3b56fd0b24980
Boot ID: 384ea93dcb604bb2ac5bec074539a7cb
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-1160.el7.x86_64
Architecture: x86-64
我試圖尋求幫助https://askubuntu.com/questions/54364/how-do-you-set-the-timezone-for-crontab和https://serverfault.com/questions/791713/what-time-zone-is-a-cron-job-using#:~:text=Cron%20job%20uses%20the%20伺服器的,%20date%20command% 20in %20終端機。但它們沒有任何用處。
答案1
如果您還沒有設定硬體比賽時間系統時間到了,然後 cron 就會開始工作了硬體時間
就我個人而言,我從不運行伺服器任何事物但UTC - 使用當地時間任何當管理多個愛好盒時,表單會變得非常複雜
我曾在運行多達 50 萬個的環境中伺服器端點(更不用說桌面)。
不要使用當地時間!