linux通过NTP服务同步国际标准时间和添加NTP任务计划
小编:动力软件园 时间:2018-06-28 11:44:27
linux时间不对,对于长期不登录的服务服务器来说,时间长了,时间不正确肯定要影响任务计划的执行。crontab用法:
[root@245 ~]# date
2018年 06月 28日 星期四 08:45:57 CST ######时间不对
[root@245 ~]# ntpdate asia.pool.ntp.org ########这里执行ntp命令同步时间。
-bash: ntpdate: command not found #####命令没找到
[root@245 ~]# yum install ntp ###########安装一下
Loaded plugins: fastestmirror
Determining fastest mirrors
* base: mirrors.njupt.edu.cn
* extras: mirrors.njupt.edu.cn
* updates: mirrors.njupt.edu.cn
base | 3.7 kB 00:00
extras | 3.4 kB 00:00
extras/primary_db | 30 kB 00:00
updates | 3.4 kB 00:00
updates/primary_db | 8.1 MB 01:25
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package ntp.x86_64 0:4.2.6p5-12.el6.centos.2 will be installed
--> Processing Dependency: ntpdate = 4.2.6p5-12.el6.centos.2 for package: ntp-4.2.6p5-12.el6.centos.2.x86_64
--> Running transaction check
---> Package ntpdate.x86_64 0:4.2.6p5-12.el6.centos.2 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================================================================================
Package Arch Version Repository Size
==============================================================================================================================================
Installing:
ntp x86_64 4.2.6p5-12.el6.centos.2 updates 600 k
Installing for dependencies:
ntpdate x86_64 4.2.6p5-12.el6.centos.2 updates 79 k
Transaction Summary
==============================================================================================================================================
Install 2 Package(s)
Total download size: 679 k
Installed size: 1.8 M
Is this ok [y/N]: y
Downloading Packages:
(1/2): ntp-4.2.6p5-12.el6.centos.2.x86_64.rpm | 600 kB 00:11
(2/2): ntpdate-4.2.6p5-12.el6.centos.2.x86_64.rpm | 79 kB 00:00
----------------------------------------------------------------------------------------------------------------------------------------------
Total 55 kB/s | 679 kB 00:12
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : ntpdate-4.2.6p5-12.el6.centos.2.x86_64 1/2
Installing : ntp-4.2.6p5-12.el6.centos.2.x86_64 2/2
Verifying : ntpdate-4.2.6p5-12.el6.centos.2.x86_64 1/2
Verifying : ntp-4.2.6p5-12.el6.centos.2.x86_64 2/2
Installed:
ntp.x86_64 0:4.2.6p5-12.el6.centos.2
Dependency Installed:
ntpdate.x86_64 0:4.2.6p5-12.el6.centos.2
Complete!
##########安装完成
[root@245 ~]# ntpdate asia.pool.ntp.org #########执行同步命令
28 Jun 09:42:16 ntpdate[7731]: step time server 182.92.12.11 offset 2877.667291 sec
[root@245 ~]# date #######查看时间
2018年 06月 28日 星期四 09:42:30 CST
[root@245 ~]#
安装完ntp服务后,开始设置任务计划,可以把经常要做的一些事放到其中,简化工作量,如每周一检查服务器的运行状态,查看报告,杀掉一些进程等等…crontab -l看一下目前所运行的计划项目。
0 8 * * * ntpdate asia.pool.ntp.org
每天早上8点同步时间。
* * * * * command
分 时 日 月 周 命令
第1列表示分钟1~59 每分钟用*或者 */1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令
crontab文件的一些例子:
30 21 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每晚的21:30重启apache。
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每月1、10、22日的4 : 45重启apache。
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每周六、周日的1 : 10重启apache。
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示在每天18 : 00至23 : 00之间每隔30分钟重启apache。
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每星期六的11 : 00 pm重启apache。
* */1 * * * /usr/local/etc/rc.d/lighttpd restart
每一小时重启apache
* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
晚上11点到早上7点之间,每隔一小时重启apache
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
每月的4号与每周一到周三的11点重启apache
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
一月一号的4点重启apache