Loading

linux定时任务

计划任务

  • 创建和管理在指定时间自动执行的任务
  • 注意:要使任务在执行时间自动执行,计划任务的服务必须是启动的
 

计划任务进程分类

  • atd进程:需要使用at命令调用,只能执行一次(一次性的)
  • crond进程:需要使用crontab命令调用,可以设置周期性执行(每天、每周)(可重复)

 

一次性计划任务

 
(1)at命令
既然at命令可以调用atd进程,那么我们就可以查看atd进程
[root@oracle ~]# ps -ef |grep atd
rpcuser    905     1  0 08:54 ?        00:00:00 rpc.statd
root      1274     1  0 08:54 ?        00:00:00 /usr/sbin/atd
root     23178 22888  0 13:53 pts/3    00:00:00 grep atd
  • 在指定的日期、时间点自动执行预先设置的一些命令操作,属于一次性计划任务
  • 系统服务的名称:/etc/init.d/atd
  • 设置格式:at [HH:MM] [yyyy-mm-dd]
    • 后面[yyyy-mm-dd]如果不写,就表示当天的执行任务
 
事例:
    
[root@oracle hang]# at 14:09
at> touch lihanghang
at> <EOT>
job 1 at 2017-06-18 14:09
[root@oracle hang]#

at后面输入时间之后直接回车,然后输入我们想要定时执行的命令,退出的时候Ctrl+D

 

 
(2)atq命令
  • 用途:查看当前设置的at任务列表(启动atd服务)
  • 同:at -l
当有一个任务还咩有执行的时候,我们通过这个命令就可以查看到
[root@oracle hang]# atq
1    2017-06-18 14:09 a root
 
(3)atrm命令
  • 用途:删除执行任务编号的at任务
  • 格式:atrm 编号
  • 同:at -d
[root@oracle hang]# at 14:23
at> touch a
at> <EOT>
job 4 at 2017-06-18 14:23
[root@oracle hang]# atq
4    2017-06-18 14:23 a root
[root@oracle hang]# atrm 4
[root@oracle hang]# atq

设定一个定时任务之后,通过命令atq查看任务列表,得到任务编号之后,通过atrm命令将定时任务删除

 

(4)at -c 任务号
  • 查看任务的具体内容
 
(5)at命令的常用格式
  • at [HH:MM]
  • at [HH:MM] [yyyy-mm-dd]
  • at now +数字 [minutes|hours|day|weeks]
at now 5 minutes:表示五分钟之后执行
 
(6)at 时间 -f文件
  • 使用文件中的命令作为计划任务
  • at now +5 minutes -f /root/test.sh
 
(7)at计划任务文件位置
  • /var/spool/at/a开头的文件
  • 任务执行完毕,文件消失
[root@oracle kernel]# cd /var/spool/at/
[root@oracle at]# ls
a00006017ce6f5  spool
 
[root@oracle at]# at -d 6
[root@oracle at]# ls
spool

当将这个执行计划删除之后这个任务文件就紧接着消失了
 
(8)at计划任务文件位置
  • /etc/at.deny:只有改名单里面的人不可以使用
  • /etc/at.allow:只有该名单里面的人可以使用
 
 

周期计划任务

 
查看crond进程信息
[root@oracle etc]# ps -ef |grep crond
root      1263     1  0 08:54 ?        00:00:00 crond
root     23419  1331  0 14:51 pts/0    00:00:00 grep crond

 

(1)crontab命令
  • 按照预先设置的时间周期(分钟、小时、天....)重复执行用户指定的命令操作,属于周期性计划任务
  • 服务名称:/etc/init.d/crond
  • 主要设置文件:用户定义的设置,位于文件:/var/spool/cron/用户名
    • 那个用户创建的计划任务,计划任务就在那个用户下面
 
(2)cron服务配置文件
  • 位于文件:/etc/crontab
  • 系统cron就在该文件定义,是为了维护系统能够正常运行的,任务的格式也和用户cron有点不同,多了一个username
  • crond计划任务里面的命令有时候可能不会执行,因为cron的环境变量PATH跟系统PATH不太一样,他的PATH的默认值为PATH=/sbin:/bin/:/usr/sbin:/usr/bin,所以这就造成很多命令不能使用,所以解决的办法有两个,可以自己设定cron的PATH环境变量,也可以用命令的绝对路径,比如ls 我们可以使用:/bin/ls
 
文件crontab的内容
 
[root@oracle etc]# cat crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# 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
 
前面四个等于,相当于定义了四个变量
文件内容很像脚本,这个的PATH路径,如果我们输入执行的命令不在PATH下的文件夹下面,使用的时候就需要使用绝对路径了
 
crontab只会在/sbin、/bin、/usr/sbin、/usr/bin下查找命令,如果命令不在这些目录中,应该要使用绝对路径
 
这里定义了格式是什么,
第一个*:代表分钟,范围是0~59
第二个*:代表了小时,范围是0~23
第三个*:代表了日期天,范围是0~31
第四个*:代表了月份,范围是1-12
第五个*:代表了星期,范围是0-6

 

(3)cron服务的日志文件
  • 位于文件:/var/log/cron
 
(4)管理cron计划任务
  • 编辑计划任务:crontab -e [-u 用户名]
  • 查看计划任务:crontab -l [-u 用户名]
  • 删除计划任务:crontab -r [-u 用户名]
 
(5)时间数值的特殊用法
  • *:表示该范围内的任意时间
  • ,:表示间隔的多个不连续时间
  • -:表示一个连续的时间范围
  • /n:指定间隔的时间频率
 
示例:
  • 0 17 * * 1-5    周一到周五每天17:00
  • 30 8 * * 1,3,5  每周一、三、五的8:30
  • 0 8-18 * * *    八点到18点整
  • 0 12 */3 * *    每隔3天的12点整
 
(6)每天早上7:50自动开启sshd服务,22:50自动关闭
  • 执行crontab -e命令之后,就会进入编辑界面
  • 添加50 7 * * * service sshd start和50 22 * * * service sshd stop两行内容保存并退出
  • 查看计划任务:
    • [root@oracle db_1]# crontab -l
    • 50 7 * * * service sshd start
    • 50 22 * * * service sshd stop
  • 查看计划任务文件位置,并查看内容
    • [root@oracle cron]# pwd
    • /var/spool/cron
    • [root@oracle cron]# cat cron
    • 50 7 * * * service sshd start
    • 50 22 * * * service sshd stop
  • 注意:这里我们要使用which命令查看计划任务命令是否在/sbin、/bin、/usr/sbin、/usr/bin下,否则我们要是会用绝对路径
 
 

周期计划任务注意事项

 
(1)资源分配不均
  • 多个计划任务同时运行
    • 如果多个任务同时运行,可能就会占用太多的cpu资源,可能就会导致系统夯住
 
(2)安全检查
  • 防止有非法计划任务
    • 定时检查一下执行任务,防止有人添加非法的计划任务
 
(3)周和日、月不可同时并存
  • 容易导致计划任务时间混乱
    • 防止我们设置的几月几号没有这个时间点
 
 
anacron程序
  • 弥补cron在系统关机后不能执行计划任务的问题
  • 不能替代cron
  • 会被每个小时被执行一次,脚本在/etc/cron.hourly
  • 按天、周或月为单位去检查系统未进行的cron任务
  • /var/spool/anacrom 
  • 服务名称:/etc/init.d/anacrond 
  • 开机时自动运行,然后将未执行的计划任务执行一遍后,anacron就会自动停止,所以我们在查看这个进程的时候就找不到,但是cron进程可以找到
    • [root@oracle etc]# ps -ef |grep anacrontab
    • root      1637  1415  0 23:57 pts/1    00:00:00 grep anacrontab
    • [root@oracle etc]# ps -ef |grep cron
    • root      1302     1  0 22:35 ?        00:00:00 crond
    • root      1643  1415  0 23:57 pts/1    00:00:00 grep cron
[root@oracle anacron]# ls
cron.daily  cron.monthly  cron.weekly
[root@oracle anacron]# pwd
/var/spool/anacron
 
(1)系统自带的计时任务存放目录,使用anacrond
[root@oracle etc]# ls cron
cron.d/       cron.daily/   cron.deny     cron.hourly/  cron.monthly/ crontab       cron.weekly/
 
(2)anacron服务配置文件
  • 位于文件:/etc/anacrontab
查看这个配置文件
 
[root@oracle etc]# cat /etc/anacrontab
# /etc/anacrontab: configuration file for anacron
# See anacron(8) and anacrontab(5) for details.
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
#period in days   delay in minutes   job-identifier   command
1    5    cron.daily        nice run-parts /etc/cron.daily
7    25    cron.weekly        nice run-parts /etc/cron.weekly
@monthly 45    cron.monthly        nice run-parts /etc/cron.monthly
 
1:任务频率
5:延迟时间
cron.daily:任务名称
/etc/cron.monthly:执行命令
run-parts:这个选项会将/etc/cron.daily目录下的文件全部执行
 
开机要执行的命令或者脚本应该添加到
  • /etc/rc.local
  • 是一个链接文件,链接到改目录下的rc.d/rc.local
[root@oracle etc]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 Dec 24 12:36 /etc/rc.local -> rc.d/rc.local
  • 所以如果我们要开机执行命令就vi这个文件,并在里面添加内容
[root@oracle etc]# vi rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local

 

 2017-06-20

posted @ 2017-06-20 00:23  李行行  阅读(462)  评论(0编辑  收藏  举报