通过 systemctl 配置开机启动同步时间服务
使用systemd来实现centos 7设置开机启动时同步时间的脚本
1、创建一个脚本文件
比如 sync_time.sh ,内容如下:
#!/bin/bash /usr/sbin/ntpdate 10.11.2.25
2、将脚本文件放在一个合适的位置
比如 /usr/local/bin/sync_time.sh ,并确保脚本有执行权限:
chmod +x /usr/local/bin/sync_time.sh
3、在路径
/etc/systemd/system/sync-time.service
创建一个systemd服务单元文件,比如 sync-time.service ,内容如下:
[unit] description=sync time on boot up after=network.target [service] type=oneshot execstart=/usr/local/bin/sync_time.sh [install] wantedby=multi-user.target
4、启用并启动这个systemd服务
systemctl enable sync-time.service systemctl start sync-time.service
这样,在centos 7系统下,每次开机时都会自动执行同步时间的脚本。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论