Linux定时任务

2018-01-04  本文已影响0人  其实我很dou

Linux定时任务

一. at一次性定时任务

at now + 2minutes
# 2min中后执行hello.sh脚本

[root@localhost ~]# at now + 2minutes
at> /root/hello.sh
# 按ctrl+d退出保存

at> <EOT>
job 3 at 2018-01-02 22:11

# 查看at
[root@localhost ~]# atq
3   2018-01-02 22:11 a root

# at -c 工作号
[root@localhost ~]# at -c 5

二. 循环定时任务crontab -e

项目 含义 范围
第一个* 一小时当中第几分钟 0-59
第二个* 一天当中第几小时 0-23
第三个* 一个月当中的第几天 1-31
第四个* 一年当中的第几个月 1-12
第五个* 一周当中的星期几 0-7(0和7都是星期日)
项目 含义
* 代表任意时间
, 代表不连续时间, 如0 8,12,16 * * * 命令 表示在8点0分, 12点0分, 16点0分 都执行一次命令
- 连续时间范围, 如 0 5 * * 1-6 命令 表示周一到周五凌晨五点0分执行命令
*/n 每隔多久执行一次, 如 * */5 * 3 * 命令 表示三月份每天每隔5小时执行一次命令

三. 系统定时任务

系统定时任务, 用于执行一些和用户不想关的系统定时任务

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
# 需要定义用户
* 5 * * * root /root/test/hello.sh
/etc/cron.daily #该目录下脚本每天执行一次
/etc/cron.weekly # 该目录下脚本每周执行一次(和上次执行时间比较 > 7 day)
/etc/cron.monthly # 该目录下脚本每月执行一次(和上次执行时间比较 > 1 months)
...
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

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# 每隔一分钟运行/etc/cron.hourly下的脚本
01 * * * * root run-parts /etc/cron.hourly
#!/bin/bash
# Skip excecution unless the date has changed from the previous run 
# 检测上次执行时间
if test -r /var/spool/anacron/cron.daily; then
    day=`cat /var/spool/anacron/cron.daily`
fi
if [ `date +%Y%m%d` = "$day" ]; then
    exit 0;
fi

# Skip excecution unless AC powered
if test -x /usr/bin/on_ac_power; then
    /usr/bin/on_ac_power &> /dev/null
    if test $? -eq 1; then
    exit 0
    fi
fi
// 设置依赖任务, 此处并不能看懂
/usr/sbin/anacron -s

上一篇下一篇

猜你喜欢

热点阅读