Linux Shell 极简教程shell命令学习笔记

Shell-Crontab调度任务

2020-05-12  本文已影响0人  傻子般白痴

1、调度任务

(1)系统任务调度

说明:

SHELL=/bin/bash   # SHELL变量指定了系统要使用哪个shell
PATH=/sbin:/bin:/usr/sbin:/usr/bin     #PATH变量指定了系统执行命令的路径 
MAILTO=root   # MAILTO变量指定了crond的任务执行信息将通过电子邮件发送给root用户,如果MAILTO变量的值为空,则表示不发送任务执行信息给用户

# 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

(2) 用户任务调度

说明:

配置文件说明:

[root@iZm5ef8xghzfk338nqbc36Z etc]# pwd
/etc
[root@iZm5ef8xghzfk338nqbc36Z etc]# ls -al cron
cron.d/       cron.daily/   cron.deny     cron.hourly/  cron.monthly/ crontab       cron.weekly/  
# 文件作用说明
# /etc/cron.deny     该文件中所列用户不允许使用crontab命令
# /etc/cron.allow    该文件中所列用户允许使用crontab命令
# /var/spool/cron/   所有用户crontab文件存放的目录,以用户名命名
# /etc/cron.daily    是每天执行一次的job
# /etc/cron.weekly   是每个星期执行一次的job
# /etc/cron.monthly  是每月执行一次的job
# /etc/cron.hourly   是每个小时执行一次的job
# /etc/cron.d        是系统自动定期需要做的任务

常见错误:

(1) crontab -l 提示 "no crontab for root"
[cause]:
    liunx服务器 第一次使用 crontab ,还没有生成对应的文件导致的
[solve]:
    使用crontab -e 进行编辑,系统会进行初始化操作。
[result]:
    no crontab for root - using an empty one
    crontab: installing new crontab

2、crond服务

说明:

操作:

[指令]: rpm -qa | grep crontab
[结果]:已安装
    [root@iZm5ef8xghzfk338nqbc36Z cron]# rpm -qa | grep crontab
    crontabs-1.11-6.20121102git.el7.noarch
[指令]:yum install -y crontabs 
/sbin/service crond start    //启动服务
/sbin/service crond stop     //关闭服务
/sbin/service crond restart  //重启服务
/sbin/service crond reload   //重新载入配置

3、crontab

说明:

语法:

crontab [选项] [参数]
[选项]
    -e:编辑该用户的计时器设置;
    -l:列出该用户的计时器设置;
    -r:删除该用户的计时器设置;
    -u<用户名称>:指定要设定计时器的用户名称。
[参数]:
    crontab文件:指定包含待执行任务的crontab文件

crontab服务:

service crond status
service crond start
ntsysv
chkconfig –level 35 crond on

crontab文件格式:

[语法]:
*       *    *    *    *      command
minute hour day month week    command
[样例]:
# 每2分钟执行一次mro.sh脚本 ,将执行结果以追加的方式添加到mroLog.txt文件中
*/2 * * * * sh /app/dag/mroMove/mro.sh >> /app/dag/mroMove/mroLog.txt  2>&1  

crontab定时任务配置:

4、shell 按秒执行

(1)通过sleep来实现

思路:

样例:

[脚本]:
#!/bin/bash
while :; do
    echo `date` >> /app/home/temp/dateLog.txt
    sleep 5
    done

[result]:
Mon May 11 10:41:28 CST 2020
Mon May 11 10:41:33 CST 2020
Mon May 11 10:41:38 CST 2020
Mon May 11 10:41:43 CST 2020
Mon May 11 10:41:48 CST 2020
上一篇 下一篇

猜你喜欢

热点阅读