2021-05-18 遇到的一点小问题
之前一直使用的指令timedatectl status
来获取时区,一般都可以正常获取:
[root@wannoo ~]# timedatectl status
Local time: Mon 2021-01-18 14:36:11 CST
Universal time: Mon 2021-01-18 06:36:11 UTC
RTC time: n/a
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
NTP service: inactive
RTC in local TZ: no
但最近经常出现因为超时而引起的时间获取失败的问题,主要错误是这两种:
[root@wannoo ~]# timedatectl status
Failed to query server: Connection timed out
[root@wannoo ~]# timedatectl status
Failed to query server: Failed to activate service 'org.freedesktop.timedate1': timed out (service_start_timeout=25000ms)
网上看了下,大部分是建议使用指令date +"%Z %z"
来获取时区,试了下,没办法获取名称。
[root@wannoo ~]# date +"%Z %z"
CST +0800
又找了下,发现通过软链文件/etc/localtime
指向的位置能获取时区名称。
[root@wannoo ~]# ls -l /etc/localtime
lrwxrwxrwx 1 root root 38 May 18 2021 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai
如果没有这个文件就当成是UTC
。
[root@wannoo ~]# ls -l /etc/localtime
ls: cannot access '/etc/localtime': No such file or directory
使用sed删除不需要的信息,最后获取到的时区名称Asia/Shanghai
就和timedatectl status
获取到的一样了。
日期时间原本是使用的Local time
,所以使用指令date +\"date\":\"%Y-%m-%d\",\"time\":\"%H:%M:%S\"
来获取也一样。