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. 【Android - 自定义View】之自定义View浅析

    1.概述 Android自定义View / ViewGroup的步骤大致如下: 1) 自定义属性: 2) 选择和设置构造方法: 3) 重写onMeasure()方法: 4) 重写onDraw()方法: ...

  2. gganimate|创建可视化动图,让你的图表会说话

    本文首发于“生信补给站”公众号,https://mp.weixin.qq.com/s/kKQ2670FBiDqVCMuLBL9NQ 更多关于R语言,ggplot2绘图,生信分析的内容,敬请关注小号. ...

  3. 动态规划--最长上升子序列(LIS)的长度

    l例如:对于[3,1,4,2,5],最长上升子序列的长度是3 arr = [3,1,4,5,9,2,6,5,0] def lis(arr): #dp[i]表示第i个位置的值为尾的数组的最长递增子序列的 ...

  4. sync.Map(在并发环境中使用的map)

    sync.Map 有以下特性: 需要并发读写时,一般的做法是加锁,但这样性能并不高,Go语言在 1.9 版本中提供了一种效率较高的并发安全的 sync.Map,sync.Map 和 map 不同,不是 ...

  5. 有奖投票丨HC2019开发者关注的TOP10问题你最想听哪个?

    目前,人工智能已经成为广大开发者重点关注的技术领域.然而,随着人工智能技术的快速发展,AI应用场景复杂度在与日俱增,算法调教也亟需不断成熟,这些都为开发者们带来了更多全新的挑战.如何快速把握前沿技术的 ...

  6. 链接脚本(Linker Script)应用实例(一)使用copy table将函数载入到RAM中运行

    将函数载入到RAM中运行需要以下三个步骤: (1)用编译器命令#pragma section "<section name>" <user functions&g ...

  7. springboot整合thymleaf模板引擎

    thymeleaf作为springboot官方推荐使用的模板引擎,简单易上手,功能强大,thymeleaf的功能和jsp有许多相似之处,两者都属于服务器端渲染技术,但thymeleaf比jsp的功能更 ...

  8. NSDateFormatter格式详细列表一览

    转自:http://www.cnblogs.com/xinus/archive/2012/10/29/NSDateFormatter_samples.html 前言:iOS开发中NSDateForma ...

  9. 尺取法two pointers

    目的:对给定的一个序列,在序列中寻找包含全部需求的.长度最小的一段子序列.一般用来解决具有单调性的区间问题. 时间复杂度:O(n) https://blog.csdn.net/lxt_lucia/ar ...

  10. nginx部署基于http负载均衡器

    nginx跨多个应用程序实例的负载平衡是一种用于优化资源利用率,最大化吞吐量,减少延迟和确保容错配置的常用技术. 环境介绍 配置nginx负载均衡器因会用到多台服务器来进行,所以下面我会用到docke ...