1.创建同步脚本文件(*.sh)
[root@localhost /]# vi syncTime.sh
2.编辑脚本文件并保存
# time.nist.gov 时间服务器地址
rdate -s time.nist.gov
#获取当前系统时间并作为日志记录到test.txt文件中
nowTime=`date "+%Y-%m-%d %H:%M:%S"`
echo "同步时间成功,当前时间:" $nowTime >> test.txt
3.修改脚本文件权限
[root@localhost /]# chmod 744 syncTime.sh
4.创建日志记录文件
[root@localhost /]# touch test.txt
5.修改日志记录文件权限
[root@localhost /]# chmod 666 test.txt
6.添加定时任务,定时执行脚本
[root@localhost /]# crontab -e
SHELL=/bin/bash
#每隔一小时执行一次
0 */1 * * * ./syncTime.sh
这样,每隔一小时就会同步一次时间服务器的时间到本地系统时间了
PS:一定要保证本地的 crond 服务处于运行状态,否则定时任务不生效,查看 crond 服务状态命令 :
[root@localhost /]# service crond status
crond (pid 1409) is running…
如上则表示 crond 服务处于运行中。
#全文毕
欢迎关注微信公众号:Javall咖啡屋
每天更新各种互联网技术(前后端、数据库、中间件、设计模式、数据结构、算法)学习心得体会