常州小程序/APP开发业务导航:

您现在的位置: 首页>站长学院>脚本代码

宝塔同步服务器时间脚本代码

来源:本站 发布时间:2023年06月09日热度:2900℃
宝塔计划任务增加一条Shell脚本脚本代码如下:echo"|-检查ntpdate命令是否就绪.."is_ntpdate=$(whichntpdate)if["$is_ntpdate"=""];thenif[-f/usr/bin/apt];then&nbs...

宝塔计划任务增加一条Shell脚本

脚本代码如下:

echo "|-检查ntpdate命令是否就绪.."
is_ntpdate=$(which ntpdate)
if [ "$is_ntpdate" = "" ];then
   if [ -f /usr/bin/apt ];then
       apt install ntpdate -y
   else
       is_dnf=$(which dnf)
       if [ "$is_dnf" = "" ];then
                yum install ntpdate -y
       fi
   fi
fi
is_ntpdate=$(which ntpdate)
is_http=0
if [ "$is_ntpdate" != "" ];then
    echo "|-正在尝试从1.pool.bt.cn同步时间..";
    ntpdate -u 1.pool.bt.cn
    if [ $? = 1 ];then
       echo "|-正在尝试从0.asia.pool.ntp.org同步时间..";
       ntpdate -u 0.asia.pool.ntp.org
   fi
else
    is_http=1
fi
if [ $? = 1 ] || [ $is_http = 1 ];then
    echo "|-正在尝试从www.bt.cn同步时间..";
    getBtTime=$(curl -sS --connect-timeout 3 -m 60 http://www.bt.cn/api/index/get_time)
    if [ "${getBtTime}" ];then
        echo "|-设置时间: "$(date -s "$(date -d @$getBtTime +"%Y-%m-%d %H:%M:%S")")
    fi
fi
echo "|-正在尝试将当前系统时间写入硬件..";
hwclock -w
echo "|-当前时间为:$(date)"
echo "|-时间同步完成!";


[声明] 本文系本网编辑转载,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请与本网邮箱 7016867@qq.com 联系,我们将在第一时间删除内容!
作者:布谷鸟网络

上一篇:我是有底线的

下一篇:网站简繁体转换js代码(如何做简繁体网站)

技术咨询

1377-5048-177

售前客服:13701594939

售后客服:0519-85656566

电商代运营:13270945888

小程序开发:18112315888

联系工程师