当咱们须要在凌晨执行一条命令或运行一个脚本的时候,咱们可能不会守在电脑旁,等时间到了去操做,常常咱们会使用任务计划cron来实现。centos
而使用任务计划每每会用到日期和时间等数值,可是咱们在安装centos的时候时间选的是对的,可是在安装完了以后咱们会发现系统的时间跟实际时间是对不上的,这是由于安装系统是采用了UTC,简单来讲UTC就是0时区的时间,是国际标准,而咱们中国是处于东八区+8时区。这个时候就须要修改系统时间了。bash
[root@localhost ~]# date 2018年 07月 27日 星期五 21:56:16 CST
[root@localhost ~]# clock 2018年07月27日 星期五 21时57分01秒 -0.810611 秒 [root@localhost ~]# hwclock 2018年07月27日 星期五 21时57分09秒 -0.219701 秒
date 020310552018.44
[root@llll ~]# date 020310552018.22 2018年 02月 03日 星期六 10:55:22 CST
date -s 07/27/18
这里是拿2018-7-27来举例。date -s 22:09:22
hwclock --systohc
时间设置完成以后就能够作任务计划了。有两个文件能够控制crontab可否被其余用户使用,/etc/cron.deny 和/etc/cron.allow 系统默认保留的是/etc/cron.deny,最好选择一个使用,避免逻辑混乱。若是不想用户lic使用crontab功能,能够将lic添加到/etc/corn.deny文件中。ssh
** 使用crontab来新建了任务计划以后,该项计划就会被记录到/var/spool/cron里面,且是以帐号来判别的。好比,root用户建了任务计划就会被写到/var/spool/cron/root中;另外cron执行的每一项工做都会被写到/var/log/cron这个日志文件中,因此若是不知道系统是否被恶意运行过cron ,能够查看该日志文件。**socket
任务计划的配置文件是/etc/crontab能够cat看一下里面的说明工具
[root@llll ~]# cat /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # For details see man 4 crontabs # Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed
能够清楚的看到,编辑一个任务计划时,第一段分钟第二段小时第三段日期第四段月份第五段礼拜几 第六段 命令 。若是每隔五分钟操做一次,就在分钟哪里*/5就能够了。centos7
[root@llll ~]# crontab -l 00 23 * * * mail li.chao11@21vianet.com < /tmp/1.txt
[root@llll ~]# crontab -r [root@llll ~]# crontab -l no crontab for root
chkconfig工具是centos7以前的版本使用的系统服务管理工具,在7以后该工具下面只有少数几个服务在里面,其余的系统服务都在systemd中rest
[root@llll ~]# chkconfig --list 注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。 若是您想列出 systemd 服务,请执行 'systemctl list-unit-files'。 欲查看对特定 target 启用的服务请执行 'systemctl list-dependencies [target]'。 iprdump 0:关 1:关 2:开 3:开 4:开 5:开 6:关 iprinit 0:关 1:关 2:开 3:开 4:开 5:开 6:关 iprupdate 0:关 1:关 2:开 3:开 4:开 5:开 6:关 netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关 network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
上面显示的这些服务,文件是在/etc/init.d下的日志
[root@llll init.d]# ls functions iprdump iprinit iprupdate netconsole network README
[root@llll init.d]# chkconfig --level 34 network off [root@llll init.d]# chkconfig --list 注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。 若是您想列出 systemd 服务,请执行 'systemctl list-unit-files'。 欲查看对特定 target 启用的服务请执行 'systemctl list-dependencies [target]'。 iprdump 0:关 1:关 2:开 3:开 4:开 5:开 6:关 iprinit 0:关 1:关 2:开 3:开 4:开 5:开 6:关 iprupdate 0:关 1:关 2:开 3:开 4:开 5:开 6:关 netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关 network 0:关 1:关 2:开 3:关 4:关 5:开 6:关
还可使用chkconfig --add 来将某个服务添加到chkconfig工具下管理或使用chkconfig --del 将某个服务从chkconfig管理工具下删除。无论删除仍是添加有一个前提是该服务有一个有效的配置文件在/etc/init.d/中code
[root@llll init.d]# cp network 222 [root@llll init.d]# ls 222 functions iprdump iprinit iprupdate netconsole network README [root@llll init.d]# cd .. [root@llll etc]# cd [root@llll ~]# chkconfig --add 222 [root@llll ~]# chkconfig --list 注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。 若是您想列出 systemd 服务,请执行 'systemctl list-unit-files'。 欲查看对特定 target 启用的服务请执行 'systemctl list-dependencies [target]'。 222 0:关 1:关 2:开 3:开 4:开 5:开 6:关 iprdump 0:关 1:关 2:开 3:开 4:开 5:开 6:关 iprinit 0:关 1:关 2:开 3:开 4:开 5:开 6:关 iprupdate 0:关 1:关 2:开 3:开 4:开 5:开 6:关 netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关 network 0:关 1:关 2:开 3:关 4:关 5:开 6:关
在centOS7 及之后的版本,systemd这个管理工具是很重要的。进程
systemctl list-unit-files
命令来查看全部的units-files显示的内容不少,不方便查看systemctl list-units --all --type=service
命令会列出全部的服务systemctl list-units --type=service
命令会列出全部激活的(active)服务systemctl -enable crond.service
systemctl -disable crond.service
systemctl status crond
systemctl stop crond
systemctl start crond
systemctl restart crond
\systemctl is-enabled crond
[root@llll ~]# systemctl is-enabled crond enabled
系统为了方便管理,用target管理unit