周期性计划任务crond讲解

crond是Linux或者unix系统的作业调度程序。运用它,在设定的时间段周期性执行某个命令或脚本。下文的例子均在centos 7.3上测试。
一、crond组件
如果centos7.3最小化安装,不一定存在crond服务,需要手动安装,安装之后手动启动并设置以后自行开机启动。
主要有cronie、cronie-anacron、crontabs三个程序包。
cronie: 主程序包,提供crond守护进程及相关辅助工具
cronie-anacron:cronie的补充程序,用于监控cronie任务执行状况,如cronie中的任务在过去该运行的时间点未能正常运行,则anacron会随后启动一次此任务
crontabs:包含CentOS提供系统维护任务
二、安装crond服务,简单设置
1、查看是不是安装cron服务:
[root@local ~]# rpm -qa cronie
2、不显示自动安装crond服务包,如果没有此服务包,则显示Error: Nothing to do
[root@local ~]# yum install cronie -y >/dev/null
[root@local ~]# rpm -qa cronie
cronie-1.4.11-14.el7_2.1.x86_64
3、查看是不是启用crond启用
[root@local ~]# systemctl status crond
crond.service – Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled)
Active: inactive (dead)
4、启动crond服务
[root@local ~]# systemctl start crond.service
5、检查crond状态
[root@local ~]# systemctl status crond
crond.service – Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled)
Active: active (running) since Sun 2017-03-26 17:50:16 CST; 3s ago
Main PID: 12895 (crond)
CGroup: /system.slice/crond.service
└─12895 /usr/sbin/crond -n
6、检查服务是不是开机自动动激活
[root@localhost ~]# systemctl is-enabled crond.service
enabled
如果不是则使用以下命令设置当前系统运行级别开机自动激活。
[root@localhost ~]# systemctl enable crond.service
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
三、crond用户作业
讲解一些在运维工作中基本的使用,如何通过命令方式添加周期性任务。
1、查看当前用户存在的周期性任务
[root@localhost ~]# crontab -l
no crontab for root
当前用户root没有周期性任务
2、单个添加当前用户的周期性任务
[root@localhost ~]# crontab
输入命令crontab回车后,等待用户输入周期性任务,输入格式为:
# 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
举个栗子:
* * * * * root /usr/bin/date >> /mnt/abc.txt
输入后回车等待输入下一个计划任务,如果结束按Ctrl+D结束
每分钟把时间当前时间追加输出至/mnt/abc.txt文件中,abc.txt文件事先存在。
[root@localhost mnt]# cat abc.txt
Sun Mar 26 12:16:01 EDT 2017
Sun Mar 26 12:17:01 EDT 2017
Sun Mar 26 12:18:01 EDT 2017
Sun Mar 26 12:19:01 EDT 2017
Sun Mar 26 12:20:01 EDT 2017
3、修改当前用户的周期性任务
[root@localhost ~]# crontab -e
回车之后会以vi方式打开当前用户的所有周期性任务,此时可以添加或删除、更改当前用户的所有周期性任务。
*/2 * * * * root /usr/bin/date >> /mnt/abc.txt
修改为每两分钟显示当前时间追加至/mnt/abc.txt中。
[root@localhost mnt]# crontab -l
*/2 * * * * root /usr/bin/date >> /mnt/abc.txt
4、删除当前用户的周期性任务
[root@localhost mnt]# crontab -r
[root@localhost mnt]# crontab -l
no crontab for root
5、交互式模式移除指定任务
[root@localhost mnt]# crontab -i
同-r一同使用
6、补充:时间格式表示
 时间表示法:
(1) 特定值
给定时间点有效取值范围内的值
(2) *
给定时间点上有效取值范围内的所有值
表示“每…”
(3) 离散取值
#,#,#
(4) 连续取值
#-#
(5) 在指定时间范围上,定义步长
/#: #即为步长
特殊时间字段表示:
 @reboot Run once after reboot.
 @yearly 0 0 1 1 *
 @annually 0 0 1 1 *
 @monthly 0 0 1 * *
 @weekly 0 0 * * 0
 @daily 0 0 * * *
 @hourly 0 * * * *
四、crontab权限管理
cront通过/etc/cront.{allow,deny}两个文件,进行权限管理,控制用户是否能执行cront任务,系统默认创建/etc/cront.deny文件,不存在/etc/cront.allow:
/etc/cront.allow 存在,则/etc/cront.deny不生效,只有文件定义的使用者才能使用 crontab 命令 ,没有在这个文件中的使用者则不能使用,即使没有写在cront.deny 当中;
/etc/cront.allow 不存在, /etc/cront.deny 生效,系统 cront.deny文件定义的使用者不能使用 cront ,而没有在这个 cront.deny 文件中的使用者则可使用 crontab 命令;
如果两个文件都不存在,只有 root 可以使用 crontab 命令 。
即白名单优先管理权限,默认没有/etc/cron.allow文件,只要创建,仅列出的允许,其他都拒绝。
以上仅讲解crond服务一部分功能,未讲解系统的周期性任务如何配置,自定义系统的周期性任务等。

原创文章,作者:chenbin,如若转载,请注明出处:http://www.178linux.com/71931

(1)
chenbinchenbin
上一篇 2017-03-27 08:36
下一篇 2017-03-27 08:48

相关推荐

  • 通过view实现智能DNS

    DNS策略解析最基本的功能是可以智能的判断访问您网站的用户,然后根据不同的访问者把您的域名分别解析成不同的IP地址,然后跟DNS服务器内部的IP表匹配一下,看看用户的类型,然后给用户返回对应的IP地址。

    Linux干货 2017-10-03
  • 马哥教育N22期第五周作业

    1、显示当前系统上root、fedora或user1用户的默认shell; [root@localhost ~]# egrep "^root|fedora|user1" /etc/passwd root:x:0:0:root:/root:/bin/bash fedora:x:1002:1002::/…

    Linux干货 2016-09-15
  • 我与Linux的第一次亲密接触

        作为一个新手,经过一周的学习,我对Linux目前有了些许的了解,从Linux的发展史中,开源共享精神深深的感染了我,也使得我对Linux的兴趣更加浓厚。对于一个一直翘计算机课的我来说,入门和基础是一项非常艰巨的任务,初期为了搭建学习的环境,也是费了不小的功夫,相信许多萌也对此有很大的感触,在这里,我来和大家分享一下如何去构建环境。…

    Linux干货 2017-03-26
  • 文本处理工具作业

    1、找出ifconfig命令结果中本机的所有IPv4地址 root@cenots6.8  ~ #  ifconfig | tr -cs '[0-9]\.' '\n' |sort -u -t&…

    Linux干货 2016-08-07
  • 正则表达式grep,sed

    HTTP 一个简单的网页文件 service iptables stop echo “<h1>I  LOVE YOU</h1>” > /var/www/html/index.html 文本排序sort sort 1.txt   排序规则先数字后字母,小写字母在前。 sort -t “:&#82…

    Linux干货 2018-03-13
  • lvs实践(lvs+nginx+mariadb+php—fpm+nfs搭建wordpress)

    lvs实践(lvs+nginx+mariadb+php—fpm+nfs搭建wordpress)   lvs实践(lvs+nginx+mariadb+php—fpm+nfs搭建wordpress) lvs_dr模型 nginx mariadb php-fpm wordpress   lvs实践(lvs+nginx+mariadb+php—f…

    2017-01-03