cron 计划任务

作用: 计划任务主要是做一些周期性的任务,目前最主要的用途是定期备份数据

Schedule one-time tasks with at. 一次性调度执行 at
Schedule recurring jobs with cron. 循环调度执行 cron
Schedule recurring system jobs.
所有计划任务执行中的输出都会以邮件的方式发送给指定用户, 除非重定向

一次性调度执行 at [了解]

语法格式:
at <TIMESPEC>
now +5min
teatime tomorrow (teatime is 16:00)
noon +4 days
5pm august 3 2018

例1:
[root@CentOS7 ~]# at now +1min
at> useradd uuuu
at> <EOT>
job 1 at Sat Mar 21 22:34:00 2015
[root@yang ~]# atq
3 Thu Mar 30 09:55:00 2017 a root
[root@CentOS7 ~]# id uuuu

例2:
[root@yang ~]# vim at.jobs
useradd u99
useradd u00
touch /`date +%F`.txt
[root@yang ~]# at now +1min < at.jobs

注: sudo执行需要考虑tty问题

循环调度执行 cron

[root@CentOS7 ~]# systemctl status crond.service
[root@CentOS7 ~]# ps aux |grep crond
root 550 0.0 0.0 126300 1648 ? Ss 10:05 0:00 /usr/sbin/crond -n
crond进程每分钟会处理一次计划任务

=====================用户级=====================
存储位置:
[root@CentOS7 ~]# ls /var/spool/cron/

管理方式:
crontab -l List the jobs for the current user.
crontab -r Remove all jobs for the current users.
crontab -e Edit jobs for the current user.
管理员可以使用 -u username, 去管理其他用户的计划任务

禁止其他用户使用计划任务
/etc/cron.deny:
alice
jack
[alice@yang ~]$ crontab -e
You (alice) are not allowed to use this program (crontab)
See crontab(1) for more information

语法格式 Job format:
Minutes Hours Day-of-Month Month Day-of-Week Command

# 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
# | | | | |
# * * * * * command

0 2 * * * /mysql_back.sh
0 2 14 * * /mysql_back.sh
0 2 14 2 * /mysql_back.sh
0 2 * * 5 /mysql_back.sh
0 2 * 6 5 /mysql_back.sh
0 2 2 * 5 /mysql_back.sh
0 2 2 6 5 /mysql_back.sh

*/5 * * * * /mysql_back.sh
0 2 1,4,6 * * /mysql_back.sh
0 2 5-9 * * /mysql_back.sh

* * * * * /mysql_back.sh
0 * * * * /mysql_back.sh
* * 2 * * /mysql_back.sh

示例:
00 02 * * * ls //每天2:00整
00 02 1 * * ls //每月1号2:00整
00 02 14 2 * ls //每年2月14号2:00整
00 02 * * 7 ls //每周日2:00整
00 02 * 6 5 ls //每年6月的周五2:00整
00 02 14 * 7 ls //每月14号2:00整 或者 每周日2:00整,这两个时间都执行
00 02 14 2 7 ls //每年2月14号2:00整 或者 每周日2:00整,这两个时间都执行

00 02 * * * ls //每天2:00整
* 02 * * * ls //每天2:00中的每一分钟
* * * * * ls //每分钟执行ls
* * 14 2 * ls //2月14号的每分钟 1440分钟

*/5 * * * * ls //每隔5分钟
00 02 1,5,8 * * ls //每月1,5,8号的2:00整
00 02 1-8 * * ls //每月1到8号的2:00整

=====================系统级=====================
临时文件的清理 /tmp /var/tmp
系统信息的采集 sar
日志的轮转(切割)logrotate
通常不是由用户定义

定义位置一:
[root@CentOS7 ~]# vim /etc/crontab //该文件中默认没有定义任何计划任务
* * * * * user-name command to be executed

定义位置二:/etc/cron.d/*
[root@CentOS7 ~]# ls /etc/cron.d
0hourly raid-check sysstat

[root@CentOS7 ~]# cat /etc/cron.d/0hourly //该文件中定义的计划任务每小时会执行
01 * * * * root run-parts /etc/cron.hourly //每小时01分以root用户执行/etc/cron.hourly目录下所有脚本

crond: 仅仅会执行每小时定义的脚本 /etc/cron.hourly

每小时具体执行哪些脚本:
[root@CentOS7 ~]# ls /etc/cron.hourly/
0anacron 0yum-hourly.cron

[root@CentOS7 ~]# cat /etc/cron.hourly/0anacron
/usr/sbin/anacron -s

[root@CentOS7 ~]# vim /etc/anacrontab //该文件是anacron执行调用的文件
#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

[root@yang ~]# ls /etc/cron.daily/
0yum-daily.cron logrotate man-db.cron mlocate
[root@yang ~]# ls /etc/cron.weekly/
[root@yang ~]# ls /etc/cron.monthly/

[root@CentOS7 ~]# ls /var/spool/anacron/
cron.daily cron.monthly cron.weekly
[root@CentOS7 ~]# cat /var/spool/anacron/cron.monthly
20170321

=====================cron log=====================
[root@CentOS7 ~]# tail /var/log/cron
Mar 29 20:01:01 CentOS7 run-parts(/etc/cron.hourly)[22456]: finished 0anacron
Mar 29 20:01:01 CentOS7 run-parts(/etc/cron.hourly)[22447]: starting 0yum-hourly.cron
Mar 29 20:01:01 CentOS7 run-parts(/etc/cron.hourly)[22462]: finished 0yum-hourly.cron
Mar 29 20:10:01 CentOS7 CROND[22563]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 29 20:20:01 CentOS7 CROND[22688]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 30 09:05:31 yang crond[1399]: (CRON) INFO (Syslog will be used instead of sendmail.)
Mar 30 09:05:31 yang crond[1399]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 15% if used.)
Mar 30 09:05:31 yang crond[1399]: (CRON) INFO (running with inotify support)
Mar 30 09:10:01 yang CROND[1924]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 30 09:20:01 yang CROND[2109]: (root) CMD (/usr/lib64/sa/sa1 1 1)

计划任务cron的更多相关文章

  1. linux的服务管理(centos6和Centos7)和网络管理(网卡配置),计划服务cron

    服务和网络 管理 init  ifcfg ens33 1.服务: Linux系统中提供的功能,统称为服务,如:at服务.cron服务.web服务.FTP服务.sshd服务等. 服务是由已经在运行的进程 ...

  2. Linux定时,计划任务cron

    假如你有一些任务要定期执行,比如清理磁盘.删除过期文件.发送邮件和提醒,这个时候可以用cron来实现. crond是后台进程,而crontab则是定制好的计划任务表. 启动与停止 查看状态/sbin/ ...

  3. 在Yii2.0中实现计划任务(cron)

    以下由我们在信易网络公司开发项目的时候终结出的一些经验 Create console application 创建命令行应用 In advance template there is already ...

  4. Ubuntu下的计划任务 -- cron的基本知识

    下面不完全: 参考:http://blog.csdn.net/cuker919/article/details/6336457 cron是一个Linux下的后台进程,用来定期的执行一些任务.因为我用的 ...

  5. 红帽学习笔记[RHCSA] 第十课[计划任务Cron与At、逻辑卷管理]

    计划任务[At & Cron Jobs] at # at 命令只能计划一次性任务但是比较方便. # 先输入时间 [root@localhost Desktop]# at 10:02 # 输入要 ...

  6. 计划任务cron,date,时间同步ntp,chrony

    取出磁盘利用率最大值 ,写个脚本,做判断,只要快满了,就报警 计划周期性执行的任务提交给crond,到指定时间会自动运行 系统cron任务:系统维护作业  /etc/crontab 用户cron任务: ...

  7. linux 的计划任务 cron

    https://serverfault.com/questions/587696/how-to-restart-php-fpm-from-cron 我也遇到了这个问题,想用cron 来启动php-fp ...

  8. 任务计划cron

    在linux中,任务计划分俩:未来时间只执行一次和周期性执行 at:未来时间只执行一次 -V 显示版本信息 -l: 列出指定队列中等待运行的作业:== atq -d: 删除指定的作业:== atrm ...

  9. 查找文件与cron计划任务

    查找文件 • 根据预设的条件递归查找对应的文件 find [目录] [条件1] [-a|-o] [条件2] ... -type  类型(f文件.d目录.l快捷方式) -name  "文档名称 ...

随机推荐

  1. python遍历所有盘符下的图片并拷贝下来

    最近在学习python,闲着无聊就试着写啦这个小的脚本,虽然有很多不足,但是还是收获不少. 该脚本的功能: ①遍历本地计算机中的所有盘符,并将名称记录下来: ②循环遍历盘符下的所有图片(当然这里可以根 ...

  2. 【Python】之format奇技淫巧的输出控制

    前置 环境:Python3.6.5 探讨点:输出print,字符串format控制, % 控制 print基础控制 简单示范: a = 1 b = '@Hello yanshanbei!' print ...

  3. 重写TabBar遇到的按钮不显示的问题

    这里的控件frame没有进行设置,无法显示 这里初始化的按钮 frame也为0, 因此 在 重写某个控件的时候 一定要调用layoutSubviews这个方法来对这个控件内部的子控件进行赋值

  4. json序列化反序列化Jackson相关注解

    1.@Transient @Transient表示该属性并非一个到数据库表的字段的映射,ORM框架将忽略该属性:如果一个属性并非数据库表的字段映射,就务必将其标示为@Transient,否则ORM框架 ...

  5. 《Windows内核安全与驱动开发》 4.4 线程与事件

    <Windows内核安全与驱动开发>阅读笔记 -- 索引目录 <Windows内核安全与驱动开发> 4.4 线程与事件 一.开辟一个线程,参数为(打印内容+打印次数),利用线程 ...

  6. Maven项目多环境之间的配置文件的切换

    前言:对于一个项目,开发和生产环境之间会使用不同的配置文件,最简单的例子就是数据库连接池的配置了.当然,可以在打包上线前对配置文件进行替换,不过这也太low了吧. 简单的pom.xml中的配置内容 比 ...

  7. windows系统tomcat上开发的j2ee程序,如何适配linux系统上奔跑的websphere7

    公司需要将几个windows系统tomcat中间件下开发的j2ee系统部署到linux系统websphere7中间件下去运行. 这就需要做系统的适配工作.由于时间比较久了,具体问题就不详细写了.把这个 ...

  8. Mybatis_多表关联查询_resultMap_集合对象_N+1方式实现

    mapper 层 提供 ClazzMapper 和 StudentMapper, ClazzMapper 查询所有班级信息, StudentMapper 根据班级编号查询学生信息. 在 ClazzMa ...

  9. [TimLinux] django CentOS7中django+httpd+mod_wsgi中文UnicodeEncodeError错误

    1. 错误 web端访问页面,页面内的view函数要写一个含有中文名字的文件,出现了这个错误.在PyCharm开发调皮环境中不存在这样的错误,把系统部署到http, mod_wsgi时出现. 2. 定 ...

  10. [TimLinux] MySQL InnoDB的外键约束不支持set default引用选项

    1. 外键 MySQL的MyISAM是不支持外键的,InnoDB支持外键,外键是MySQL中的三大约束中的一类:主键约束(PRIMARY KEY),唯一性约束(UNIQUE),外键约束(FOREIGN ...