定时任务


备份

2.其他重复性工作 同步时间3.规则+脚本
4.crond (crontab) 定时任务软件(软件包 cronie)
a+d运行一次
anacron 非724小时运行的服务器
1.3 查看安装包及内容

[root@oldboyedu50 ~]# rpm -qa cronie 是否安装上
cronie-1.4.4-16.el6_8.2.x86_64
[root@oldboyedu50 ~]# rpm -ql cronie 安装的内容
/etc/cron.d 
/etc/cron.d/0hourly
/etc/cron.deny

/etc/pam.d/crond
/bin/crontab
/usr/sbin/crond
/usr/share/doc/cronie-1.4.4
/usr/share/doc/cronie-1.4.4/AUTHORS
/usr/share/doc/cronie-1.4.4/COPYING
/usr/share/doc/cronie-1.4.4/ChangeLog
1.4 系统定时任务
日志允许/切割 cletc /cron.daily (每天)
/etc/cron.hourly/ 系统定时每小时运行这个目录的内容
/etc/cron.daily/ 系统定时每天运行这个目录的内容
/etc/cron.weekly/ 系统定时每周运行这个目录的内容
/etc/cron.monthly/ 系统定时每月运行这个目录的内容
系统定时任务+logrotate命令,完成对日志切割,对日志轮询
[root@oldboyedu50 ~]# ll -d /etc/cron.
drwxr-xr-x. 2 root root 4096 Jul 19 2018 /etc/cron.d
drwxr-xr-x. 2 root root 4096 Jul 19 2018 /etc/cron.daily
-rw-------. 1 root root 0 Aug 24 2016 /etc/cron.deny



drwxr-xr-x. 2 root root 4096 Jul 19 2018 /etc/cron.hourly
drwxr-xr-x. 2 root root 4096 Jul 19 2018 /etc/cron.monthly
drwxr-xr-x. 2 root root 4096 Sep 27 2011 /etc/cron.weekly

查看定时任务

Crontab –l ==cat /var/spool/cron/root

1.6 设置定时任务
Crontab –e 设置定时任务 相当于vim /var/spool/cron/root
1.7 检查定时任务是否运行? Crond

[root@oldboyedu50 ~]# ps -ef |grep crond 进程是否运行
root 1603 1 0 10:03 ? 00:00:07 crond
root 23812 23682 0 13:29 pts/1 00:00:00 grep --color crond

1.8 是否开机自启动

[root@oldboyedu50 ~]# chkconfig |grep cron 开机自启动
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off

1.9 显示3:on第一列

[root@oldboyedu50 ~]# chkconfig |awk '/3:on/{print $1}' 利用管道取列
abrt-ccpp 检查,设置系统各种服务
abrtd
acpid
atd
auditd

blk-availability
cpuspeed
crond

 定时任务配置文件格式:

定时任务配置文件格式:
什么时候 做什么 
分时日月周

注:没有具体说就用“” 代替
2.1 每天的上午8点30分,来学校上车(go to school)
30 08 
go to school
2.2 每天晚上12点准时,回家自己开车(go to bed)
00 00 go to bed
2.3 每分钟显示自己的名字追加到/tmp/name.log中

##命令
[root@oldboyedu50 ~]# echo wly >>/tmp/name.log 先添加内容
[root@oldboyedu50 ~]# cat /tmp/name.log 检查
wly
[root@oldboyedu50 ~]#

###书写定时任务

[root@oldboyedu50 ~]# crontab –l 查看定时任务

#priint name oldboy at 20180808
# echo oldboy >>/tmp/name.log
#sync time oldboy at 20180808
/2 /usr/sbin/ ntpdate ntp1.aliyun.com

##检查文件内容 检查日志

[root@oldboyedu50 ~]# cat /tmp/name.log 
#wly

        • [root@oldboyedu50 ~]# cat /tmp/name.log 
          #wly
          echo wly >>/tmp/name.log
          [root@oldboyedu50 ~]# ntpdate ntp1.aliyun.com 时间同步
          Error resolving ntp1.aliyun.com: Servname not supported for ai_socktype (-8)
          28 May 13:56:12 ntpdate[23927]: Can't find host ntp1.aliyun.com: Servname not supported for ai_socktype (-8)
          [root@oldboyedu50 ~]# which ntpdate
          /usr/sbin/ntpdate
          [root@oldboyedu50 ~]# /usr/sbin/ntpdate ntp1.aliyun.com 全路径
          Error resolving ntp1.aliyun.com: Servname not supported for ai_socktype (-8)
          28 May 14:00:58 ntpdate[23947]: Can't find host ntp1.aliyun.com: Servname not supported for ai_socktype (-8)
          2.4 每2分钟同步下系统时间

           


###定时任务内容

###写入定时任务

[root@oldboyedu50 ~]# crontab –l 
#priint name oldboy at 20180808
# echo oldboy >>/tmp/name.log
#sync time oldboy at 20180808
/2 /usr/sbin/ ntpdate ntp1.aliyun.c

####检查

[root@oldboyedu50 ~]# tail -5 /var/log/cron 查看日志
May 28 14:04:01 oldboyedu50 CROND[23969]: (root) CMD (/usr/sbin/ ntpdate ntp1.aliyun.com)
May 28 14:06:01 oldboyedu50 CROND[23976]: (root) CMD (/usr/sbin/ ntpdate ntp1.aliyun.com)
May 28 14:08:01 oldboyedu50 CROND[23984]: (root) CMD (/usr/sbin/ ntpdate ntp1.aliyun.com)
May 28 14:10:01 oldboyedu50 CROND[23992]: (root) CMD (/usr/lib64/sa/sa1 1 1)
May 28 14:10:01 oldboyedu50 CROND[23993]: (root) CM

2.5 定时任务符号

每 命令
/n /10 * 每隔/每十分钟

每隔1分钟
/1 每隔1分钟
1 每隔1分钟 
1 每隔1小时
/2 每隔2分钟
2.6 每天的上午7点到上午11点 每2小时运行CMD命令
00 7-11/2 cmd
00 7,11/2 cmd

 

posted @ 2018-09-25 20:35  w浅时光  阅读(247)  评论(0编辑  收藏  举报