1.任务调度

Linux下的任务调度分为两类:系统任务调度和用户任务调度。
系统任务调度:系统周期性所要执行的工作,比如写缓存数据到硬盘,存放在/etc目录下的crontab文件中。
用户任务调度:用户定期要执行的工作,比如用户数据备份、定时邮件提醒,存放在/var/spool/cron目录中,其文件名与用户名一致。
使用者权限文件如下:/etc/cron.deny 该文件中所列用户不允许使用crontab,/etc/cron.allow 该文件中所列用户允许使用crontab命令。

2.crond

crond是一个daemon类程序,路径为/usr/sbin/crond。默认会以后台方式启动,systemd方式启动crond。

[root@master ~]# crond -h
Usage:crond [options]Options:-h         print this message -i         deamon runs without inotify support-m <comm>  off, or specify prefered client for sending mails-n         run in foreground-p         permit any crontab-P         use PATH="/usr/bin:/bin"-c         enable clustering support-s         log into syslog instead of sending mails-x <flag>  print debug information

Debugging flags are: ext,sch,proc,pars,load,misc,test,bit

crond [-n] [-P] [-x flags]
-n:让crond以前端方式运行,即不依赖于终端。
-P:不重设环境变量PATH,而是从父进程中继承。
-x:设置调试项,flags是调试方式,比较有用的方式是test和sch,即"-x test"和"-x sch"。
:其中test调试将不会真正的执行,sch调试显示调度信息,可以看到等待时间。

3.crondtab

crondtab是管理crontab file的工具,而crontab file是定义定时任务条目的文件。crontab file存在于多处,包括系统定时任务文件/etc/crontab和/etc/cron.d/*,还有独属于各用户的任务文件/var/spool/cron/USERNAME。

#没有help选项
[root@master ~]# crontab --help
crontab: invalid option -- '-'
crontab: usage error: unrecognized option
Usage:crontab [options] filecrontab [options]crontab -n [hostname]Options:-u <user>  define user-e         edit user's crontab-l         list user's crontab-r         delete user's crontab-i         prompt before deleting-n <host>  set host in cluster to run users' crontabs-c         get host in cluster to run users' crontabs-s         selinux context-x <mask>  enable debugging
Default operation is replace, per 1003.2

-l:列出定时任务条目
-r:删除当前任务列表终端所有任务条目
-i:删除条目时提示是否真的要删除
-e:编辑定时任务文件,实际上编辑的是/var/spool/cron/*文件
-u:操作指定用户的定时任务

[root@master ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# 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

定义了3个变量,其中一个是SHELL、PATH、MAILTO。每个任务条目分为6段,每段以空格分隔,之所以多了user-name段是因为/etc/crontab为系统定时任务文件,一般定时任务是没有该段的。.前五段为时间的设定段,分别表示"分时日月周",第六段为所要执行的命令或脚本任务段。
在时间定义段中,使用"*“表示每单位,即每分钟,每小时,每天,每月,每周几(仍然是每天)。时间单位的意思;使用逗号”,"来表示枚举,例如定义"0,30,50 * * * “表示每个时辰的整点、第30分钟和第50分钟都执行该任务;使用”-“定义范围,可以结合逗号使用。如分钟段定义了"00,20-30,50"表示每个时辰的整点、第20到30分钟的每分钟、第50分钟都执行该任务。在小时段定义了"0-13/2"表示在"0/2/4/6/8/10/12"点才满足时间定义。使用”/N"表示每隔多久的意思。例如"00 */2 * * "表示在每天每隔两小时的整点执行该任务(严格地说是0-23/2,也就是0,2,4,…,22。如果定义的日和周冲突了,则会多次执行(不包括因为号导致的冲突)。例如每月的15号执行该任务,同时又定义了周三执行该任务,正常无冲突情况下,将在周三和每月15号执行,但如果某月的15号同时是周三,则该任务在此日执行两次。因此,应该尽力避免同时定义周和日的任务。

"%"表示换行,且第一个%后的所有字符串将当作命令的标准输入。

* * * * * /bin/cat >>/tmp/crond.txt %"the first %%cron entry%"该任务输出:"the firstcron entry
"

以时间定义文件名时,使用反斜杠转义%。

* * * * * cp /etc/fstab /tmp/`date +\%Y-\%m-\%d`.txt
4. crondtab file

crontab file存在于/etc/crontab和/etc/cron.d/*,还有独属于各用户的任务文件/var/spool/cron/USERNAME。
执行crontab -e命令编辑当前用户的crontab file,当前为root用户,则编辑的是/var/spool/cron/root文件。 在crontab file中设置环境变量,方式为"name=value",value中出现的空格必须使用引号。除了LOGNAME/HOME/SHELL变量之外,如果设置了发送邮件,则crond还会寻找MAILTO变量。如果设置了MAILTO,则邮件将发送给此变量指定的地址,如果MAILTO定义的值为空(MAILTO=""),将不发送邮件,其他所有情况邮件都会发送给crontab file的所有者。
在系统定时任务文件/etc/crontab中,默认已定义PATH环境变量和SHELL环境变量,其中PATH=/sbin:/bin:/usr/sbin:/usr/bin。crond daemon每分钟检测一次crontab file看是否有任务计划条目需要执行。

5. crond 调试
[root@master ~]#  crontab -e
*/20 * * * * /sbin/ntpdate -u pool.ntp.org > /dev/null 2>&1
* * * * * echo "hello world" >>/tmp/hello.txt
[root@master ~]# crond -x test
debug flags enabled: test
crond: can't lock /var/run/crond.pid, otherpid may be 10068: Resource temporarily unavailable
log_it: (CRON 66507) DEATH (can't lock /var/run/crond.pid, otherpid may be 10068): Resource temporarily unavailable
[root@master ~]# systemctl stop crond
[root@master ~]# crond -x test
debug flags enabled: test
[66644] cron started
log_it: (CRON 66644) INFO (Syslog will be used instead of sendmail.)
log_it: (CRON 66644) INFO (RANDOM_DELAY will be scaled with factor 42% if used.)
log_it: (CRON 66644) INFO (running with inotify support)
log_it: (CRON 66644) INFO (@reboot jobs will be run at computer's startup.)
log_it: (root 66685) CMD (echo "hello world" >>/tmp/hello.txt)
log_it: (root 66684) CMDOUT (debug DTEST is on, not exec'ing command.)
log_it: (root 66684) CMDOUT (   cmd='echo "hello world" >>/tmp/hello.txt' shell='/bin/sh')
[root@master ~]# crond -x sch
debug flags enabled: sch
[66753] cron started
log_it: (CRON 66753) INFO (Syslog will be used instead of sendmail.)
log_it: (CRON 66753) INFO (RANDOM_DELAY will be scaled with factor 76% if used.)
log_it: (CRON 66753) INFO (running with inotify support)
[66753] GMToff=28800
log_it: (CRON 66753) INFO (@reboot jobs will be run at computer's startup.)
[66753] Target time=1583355240, sec-to-wait=50
user [root:0:0:...] cmd="echo "hello world" >>/tmp/hello.txt"
[66753] Target time=1583355300, sec-to-wait=60
Minute-ly job. Recording time 1583326441
log_it: (root 66796) CMD (echo "hello world" >>/tmp/hello.txt)
[root@master ~]# crond -x test,sch
5. 精确到秒定制任务计划

5.1

[root@master ~]# cat /tmp/a.sh
#!/bin/bash
#
PATH="$PATH:/usr/local/bin:/usr/local/sbin"
for ((i=1;i<=20;i++));do
ls /tmp
sleep 3
done[root@master ~]# cat /var/spool/cron/lisi
* * * * * /bin/bash /tmp/a.sh

5.2

cat /var/spool/cron/blueicex
* * * * * ls /tmp
* * * * * sleep 3 && ls /tmp

————Blueicex 2020/03/04 21:21 blueice1980@126.com

Linux—— crond相关推荐

  1. 老男孩Linux Crond定时任务练习题

    Linux Crond定时任务练习题 Crond是系统中用来定期执行命令或指定程序任务的一种服务或软件. 严格的说,linux系统下的定时任务软件不少,例如:at.crontab.anacron,其中 ...

  2. Linux Crond服务

    Linux Crond服务 标题 内容 crond 定义,作用,特点 crond 配置参数说明 crond crontab命令方法 crond crond服务的注意事项 crond定义,作用,特点 c ...

  3. linux crond命令

    crond 是linux用来定期执行程序的命令.当安装完成操作系统之后,默认便会启动此任务调度命令分锺会定期检查是否有要执行的工作,如果有要执行的工作便会自动执行该工作.而linux任务调为以下两类: ...

  4. Linux crond实例

    linux系统的定时任务: 1:linux系统自身定期执行的任务工作:系统周期性执行的任务工作,如轮询系统日志,备份系统数据,清理系统缓存等. [root@10-4-5-9 ~]# ll /var/l ...

  5. linux Crond 执行预定任务

    1.循环执行的计划任务 linux下面有atd和crond两种计划任务,其中,atd服务使用的at命令只能执行一次,而crond服务使用的crontab定义的命令,是循环作用的,所以crond才符合我 ...

  6. Linux 关闭crond服务,linux crond 服务配置详解

    cron 是linux的内置服务,可以用以下的方法启动.关闭这个服务: 引用: /sbin/service crond start //启动服务 /sbin/service crond stop // ...

  7. Linux crond命令使用介绍

    目录 前言 一.crond简介 系统任务调度 用户任务调度 二.crond服务 三.crontab命令详解 1.命令格式 2.命令功能 3.命令参数 4.常用方法 1). 创建一个新的crontab文 ...

  8. linux crond进程多,Linux之定时任务Crond详解

    定时任务Crond介绍 Crond是linux系统中用来定期执行命令/脚本或指定程序任务的一种服务或软件,一般情况下,我们安装完Centos5/6 linux操作系统之后,默认便会启动Crond任务调 ...

  9. linux cron crond 区别,linux crond计划任务详解

    1.循环执行的计划任务 linux下面有atd和crond两种计划任务,其中,atd服务使用的at命令只能执行一次,而crond服务使用的crontab定义的命令,是循环作用的,所以crond才符合我 ...

最新文章

  1. Matlab绘图高级部分
  2. zabbix1.8和2.0版本通用的安装脚本
  3. Openwebmail在Ubuntu Linux上的安装过程
  4. 对于C#里面的this与base
  5. 7月新的开始 - Axure学习03 - 布尔运算、表单元件
  6. Spring MVC 多选框 绑定 Entity 中的 list 属性
  7. matlab中给图像加几个矩形框_没想到!PPT中的这个效果,用好了,简直就是渣图美化器...
  8. oracle外网监听端口,oracle 11g 修改默认监听端口1521
  9. Python 迭代器和 C++ 迭代器,最大的不同竟然是......
  10. AD RMS高可用(二)为rms群集服务器申请证书
  11. 硬币找零,最长上升子序列,背包问题等动态规划问题详解
  12. linux 安装yum 安装php
  13. Android 动态壁纸
  14. 廊坊市博实计算机网络工程有限公司,IP网络终端功放T-7760(含数字IP网络平台终端嵌入软件)...
  15. 循环矩阵求特征值的方法
  16. matlab 一榀框架,一榀框架(一榀框架的选取依据)
  17. 在Redhat9下安装Oracle9
  18. 第二届翼支付杯大数据建模大赛-信用风险用户识别Baseline 线上0.65+稳进复赛
  19. oracle12c 配置监听,redhat上oracle 12c配置监听
  20. Conflux TokenGazer AMA活动内容回顾

热门文章

  1. 【软件测试】2021年软件测试领域常用工具总结(2)-接口测试,UI测试工具篇
  2. 常用模块fileinput学习
  3. TS 中 type 和 interface 的区别
  4. Python将JSON文件转Excel的方法
  5. 写一篇2000字的关于磁浮列车研究背景及意义的文章
  6. 微信朋友圈“文字发”操作技巧,轻松实现不带图发布
  7. 贴片微型滚珠振动开关的结构原理
  8. 离散傅里叶变换 - 快速计算方法及C实现 - 第一篇
  9. 全球与中国海绵钛市场深度研究分析报告
  10. Django2.0异常:Specifying a namespace in include() without providing an app_name is not supported.