定时任务
发布时间:2020-12-14 04:43:46 所属栏目:大数据 来源:网络整理
导读:1.1简介 1.定时任务种类crond(crontab)定时任务软件(软件包cronie)atd运行一次anacron非7*24小时运行2.病毒利用定时任务系统定时任务,系统会自动运行里面的内容系统中毒的时候,会把内容放到这里面,以便病毒文件被删除了,会进行自动下载/etc/cron.daily/e
1.1简介
1.定时任务种类 crond(crontab)定时任务软件(软件包cronie) atd运行一次 anacron非7*24小时运行 2.病毒利用定时任务 系统定时任务,系统会自动运行里面的内容 系统中毒的时候,会把内容放到这里面,以便病毒文件被删除了,会进行自动下载 /etc/cron.daily /etc/cron.hourly /etc/cron.monthly /etc/cron.weekly 1.2定时任务+logrotate日志轮询1.2.1系统日志轮询功能的实现通过每天执行logrotate程序进行日志轮询。 每天执行logrotate是因为logrotate放在了 /etc/cron.daily/下,定时器会每天执行该目录下的程序 [[email?protected] ~]# ll /etc/cron.daily/ total 24 -rwx------. 1 root root 180 Jul 10 2003 logrotate -rwx------. 1 root root 927 Mar 22 2017 makewhatis.cron -rwx------. 1 root root 189 Jan 26 2015 mlocate.cron -rwxr-xr-x. 1 root root 2126 Jul 19 2013 prelink -rwxr-xr-x. 1 root root 563 Nov 23 2013 readahead.cron -rwxr-xr-x. 1 root root 433 Nov 7 2015 tmpwatch 1.2.2logrotate下面详细介绍logrotate这个系统中具有日志轮询功能的程序 [[email?protected] ~]# cat /etc/cron.daily/logrotate #!/bin/sh /usr/sbin/logrotate /etc/logrotate.conf EXITVALUE=$? if [ $EXITVALUE != 0 ]; then /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]" fi exit 0 1.告知logrotate的配置文件 为/etc/logrotate.conf 2.分析 /etc/logrotate.conf [[email?protected] ~]# cat /etc/logrotate.conf # see "man logrotate" for details # rotate log files weekly weekly ---每周进行一次日志转储 # keep 4 weeks worth of backlogs rotate 4 ---保留4份备份的日志 # create new (empty) log files after rotating old ones create ---创建新的日志文件,默认是 create 600 root root # use date as a suffix of the rotated file dateext ---指定转储文件的扩展名为secure-20190224格式 # uncomment this if you want your log files compressed #compress ---如果想要对转储的日志文件进行压缩,可以取消注释 # RPM packages drop log rotation information into this directory include /etc/logrotate.d ---包含该路径下的配置文件,默认该路径下配置文件中的设置优先于本配置文件 下面是对wtmp和btmp设置的转储方式 # no packages own wtmp and btmp -- we‘ll rotate them here /var/log/wtmp { monthly create 0664 root utmp minsize 1M rotate 1 } /var/log/btmp { missingok monthly create 0600 root utmp rotate 1 } # system-specific logs may be also be configured here. 3.系统日志配置 在/etc/logrotate.d/syslog [[email?protected] ~]# cat /etc/logrotate.d/syslog /var/log/cron /var/log/maillog /var/log/messages /var/log/secure /var/log/spooler { sharedscripts postrotate /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true ---执行完日志转储,执行的脚本程序 endscript } [[email?protected] ~]# ll /var/log/cron /var/log/secure* -rw------- 1 root root 257184 Mar 5 00:36 /var/log/cron -rw------- 1 root root 34211 Mar 5 00:11 /var/log/secure -rw-------. 1 root root 1927 Feb 24 08:16 /var/log/secure-20190224 4.根据对以上logrotate的分析,可知系统对 /var/log/cron /var/log/maillog /var/log/messages /var/log/secure /var/log/spooler 这几个日志的转储是每周转储一次,保留4份备份,新的日志文件权限为600 root root,备份文件的名称为***-20190224格式,不对转储的日志文件进行压缩,转储后重启syslogd 1.3用户定时任务crontab -l列表,查看用户的定时任务 crontab -e编辑,编辑用户的定时任务 /var/spool/cron/root (root是用户名 root用户的定时任务) 1.4用户定时任务的使用1.定时任务依赖的软件是否正在运行 [[email?protected] cron]# service crond status crond (pid 1563) is running... You have new mail in /var/spool/mail/root [[email?protected] cron]# ps -ef|grep crond root 1563 1 0 Feb27 ? 00:00:00 crond root 4767 4560 0 02:35 pts/2 00:00:00 egrep --color=auto crond 2.查看crond是否开机自启动 [[email?protected] cron]# chkconfig --list|grep crond crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off 1.5crontab命令怎么使用1.crontab -l列表,查看用户的定时任务 cat /var/spool/cron/root 2.crontab -e编辑,编辑用户的定时任务 vi /var/spool/cron/root 3./var/spool/cron/root (root是用户名 root用户的定时任务) 4.为何使用crontab命令,不直接编辑文件 1)因为有语法检查功能,能检查格式对不对 2)方便 1.6定时任务相关文件* /var/spool/cron 定时任务的配置文件所在的目录 * /var/log/cron定时任务日志文件,运行过程的记录 * /etc/cron.deny哪些用户禁止使用定时任务 1.7定时任务格式与常见写法1.7.1特殊符号介绍*每 /n */10 * * * * 每隔十分钟 -从哪里来到哪里去07-08,分隔 1.7.2样例*/5 * * * * ntpdate ntp1.aliyun.com 步骤一:首先验证命令的正确性,在命令行执行ntpdate ntp1.aliyun.com 步骤二:crontab -e书写定时任务 步骤三:查看/var/log/cron 步骤四:验证系统时间date是否更改正确 1. * 07-11 * * * * 这里表示上午7点到11点,每小时每分钟运行CMD 2. 00 07-11 * * * 表示整点的时候,每几个小时运行一次的时间,分钟写上00 3. 00 17,19,20 * * * 表示17点,19点,20点运行CMD 4. * * * * * echo "oldboy">>/oldboy/oldboy.txt 每分钟把自己的名字追加到/oldboy/oldboy.txt 5. 23点,0点到7点,每小时重启nginx 1.* 23,00-07/1 * * * /application/nginx/sbin/nginx -s reload 这个错误,表示23点,0点到7点的每分钟都重启,重启到停不下来 2.00 23,00-07 * * * /application/nginx/sbin/nginx -s reload 这个正确 6. 00 00 * * * 表示半夜12点 1.8定时任务的书写流程* 1.命令行测试 * 2.把命令写到脚本中 * 3.测试下脚本是否可以用 * 4.写定时任务 * 5.检查结果 1.9 9句箴言1.9.1定时任务规则之前加注释1.9.2使用脚本替代命令行定时任务超过两条命令都用脚本 每分钟显示系统时间到time.log中 1)验证命令 [[email?protected] ~]# echo "`date +%F`">>/oldboy/time.log 2)写脚本 [[email?protected] opt]# cat date.sh echo "`date +%F`">>/oldboy/time.log 执行脚本 [[email?protected] opt]# sh date.sh 3)写定时任务 * * * * * /bin/sh /opt/date.sh 4)检查 [[email?protected] opt]# cat /oldboy/time.log 2019-02-28 2019-02-28 2019-02-28 1.9.3定时任务中date命令%有特殊含义的,默认是回车之意[[email?protected] opt]# crontab -l * * * * * echo "`date +%F`">>/oldboy/time.log 看日志 [[email?protected] cron]# cat /var/log/cron Feb 28 16:38:01 oldboyedu-02 CROND[5611]: (root) CMD (echo "`date +") %在定时任务中是有特殊含义的,认为是回车,需要写成 * * * * * echo "`date +%F`">>/oldboy/time.log 所以在定时任务中最好直接写脚本 1.9.4运行脚本一定要用/bin/sh或sh默认情况下,新建的文件权限为644,是没有执行权限的,./执行是没有权限的,但是sh执行就可以解决该问题 1.9.5.定时任务中,命令或脚本结果(正确的和错误的)定向到黑洞中,>/dev/null 2>&1或者追加到文件中>>/oldboy/time.log 2>&11)邮件的软件没有开启,大量的小文件堆积在/var/spool/postfix/maildrop/---inode满了 以上只会在定时任务中存在命令异常的情况下导致inode已满,定时任务没有异常的情况下不会导致inode满 解决方法:把定时任务中每行定时任务的正确和错误的结果重定向到文件或/dev/null中就好了。 2)邮件的软件开启了,会不断给root用户发邮件,并且提示 You have new mail in /var/spool/mail/root 关闭邮件服务: [[email?protected] cron]# service postfix status master (pid 1522) is running... [[email?protected] cron]# service postfix stop Shutting down postfix: [ OK ] [[email?protected] cron]# chkconfig postfix off 1.9.6避免不必要的程序及命令输出tar zcf 就好,就不要tar zcvf 了 1.9.7打包压缩使用相对路径(切到目标目录的上一级打包目标)cd / &&tar zcf /tmp/ser-$(date +%F).tar.gz etc/services 1.9.8定时任务脚本中的程序文件,尽量使用绝对路径* * * * * echo "oldboy">>oldboy.txt 用户的定时任务中,默认存放在当前用户的家目录下 系统的定时任务,是存放在根目录下 1.9.9系统与命令位置有关的环境变量问题Java环境变量问题 每分钟显示当前系统时间 年-月-日_周和当前系统的ip地址,追加到ip.log中 [[email?protected] opt]# cat ip.sh echo "`date +%F_%w`","`ifconfig eth0|sed -nr ‘2s#^ .*dr:(.*) B.*#1#gp‘`">>/oldboy/ip.log [[email?protected] opt]# crontab -l #print date+ip to file by vita at 20190228 * * * * * sh /opt/ip.sh >/oldboy/ip.log 2>&1 [[email?protected] opt]# cat /oldboy/ip.log /opt/ip.sh: line 1: ifconfig: command not found 2019-02-28_4,为什么ifconfig: command not found 定时任务运行脚本的时候,可以识别的PATH只有/usr/bin和/bin,而ifconfig在/sbin/ifconfig 解决方法: 1).命令使用绝对路径 [[email?protected] opt]# cat ip.sh echo "`date +%F_%w`"," `/sbin/ifconfig eth0|sed -nr ‘2s#^ .*dr:(.*) B.*#1#gp‘`">>/oldboy/ip.log 2).在脚本开头重新定义一下PATH [[email?protected] opt]# cat ip.sh export PATH= /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin echo "`date +%F_%w`","`ifconfig eth0|sed -nr ‘2s#^ .*dr:(.*) B.*#1#gp‘`">>/oldboy/ip.log PATH中的内容 bin /bin /usr/bin /usr/local/bin sbin /sbin /usr/sbin /usr/local/sbin 1.10如何删除大量小文件[[email?protected] test]# touch {1..500000}.txt -bash: /bin/touch: Argument list too long [[email?protected] test]# echo {1..500000}.txt|xargs touch [[email?protected] ~]# find / -type d -size +1M|xargs ls -lhd dr-xr-xr-x. 25 root root 1.4M Feb 28 16:29 / drwxr-xr-x 2 root root 15M Feb 28 18:25 /oldboy/test [[email?protected] ~]# rm -rf /oldboy/test/* -bash: /bin/rm: Argument list too long [[email?protected] ~]# ls /oldboy/test/|xargs rm -rf 如果还是不可以,就批量删除,rm -rf A* (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |