周期性计划任务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

相关推荐

  • 网络N23期第二周心得

    1. Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。 cp 文件复制        常用选项:            -i:交互式            -r, -R: 递归…

    Linux干货 2016-12-05
  • 文件元数据信息的含义、查看方法,和文件时间戳信息的修改方法

    文件数据分成两类 元数据,英文叫metadata,是数据的属性; 数据,英文叫data,是数据本身; 使用stat命令查看元数据信息 [0][root@localhost mylinux]# stat /etc/passwd File: ‘/etc/passwd’ Size: 889 Blocks: 8 IO Block: 4096 regular file…

    Linux干货 2018-03-01
  • 文件的归档和压缩

    文件的归档和压缩 •一、tar命令使用 •二、其他压缩方式 •三、进程管理基本概念。 前言: 本节主要介绍文件的归档和压缩相关方法。归档和压缩有利于linux系统中文件的管理和磁盘空间的利用,善于利用归档和压缩能为我们工作中带来很多便捷。另外将简单介绍进程的一些概念,方便下一节进程管理内容的学习。 一、 tar命令使用(tar命令用于文件…

    2017-04-16
  • httpd

      httpd http服务器程序:   httpd (apache) nginx lighttpd httpd的特性:   高度模块化: core + modules DSO:dynamic shared object(模块的动态加载) MPM:Multipath processing Modules (多路处理模块) &nb…

    Linux干货 2017-06-04
  • Linux 文 本 处 理 工 具

    Linux 文 本 处 理 工 具 一.学习大纲: ◎各种文本工具来查看、分析、统计文本文件 文件内容查看工具:cat, tac,rev,more,less 文件截取:head和tail 按列抽取:cut,paste 分析文本的工具:wc , sort , uniq,diff和patch 命令使用练习题 ◎文本过滤与处理工具: grep与正则表达式…

    Linux干货 2016-08-05
  • nginx

    http http协议:web服务器(类似于httpd)、http reverse proxy(类似于httpd)、imap/pop3 reverse proxy NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/P…

    Linux干货 2017-06-25