计划任务cron
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的更多相关文章
- linux的服务管理(centos6和Centos7)和网络管理(网卡配置),计划服务cron
服务和网络 管理 init ifcfg ens33 1.服务: Linux系统中提供的功能,统称为服务,如:at服务.cron服务.web服务.FTP服务.sshd服务等. 服务是由已经在运行的进程 ...
- Linux定时,计划任务cron
假如你有一些任务要定期执行,比如清理磁盘.删除过期文件.发送邮件和提醒,这个时候可以用cron来实现. crond是后台进程,而crontab则是定制好的计划任务表. 启动与停止 查看状态/sbin/ ...
- 在Yii2.0中实现计划任务(cron)
以下由我们在信易网络公司开发项目的时候终结出的一些经验 Create console application 创建命令行应用 In advance template there is already ...
- Ubuntu下的计划任务 -- cron的基本知识
下面不完全: 参考:http://blog.csdn.net/cuker919/article/details/6336457 cron是一个Linux下的后台进程,用来定期的执行一些任务.因为我用的 ...
- 红帽学习笔记[RHCSA] 第十课[计划任务Cron与At、逻辑卷管理]
计划任务[At & Cron Jobs] at # at 命令只能计划一次性任务但是比较方便. # 先输入时间 [root@localhost Desktop]# at 10:02 # 输入要 ...
- 计划任务cron,date,时间同步ntp,chrony
取出磁盘利用率最大值 ,写个脚本,做判断,只要快满了,就报警 计划周期性执行的任务提交给crond,到指定时间会自动运行 系统cron任务:系统维护作业 /etc/crontab 用户cron任务: ...
- linux 的计划任务 cron
https://serverfault.com/questions/587696/how-to-restart-php-fpm-from-cron 我也遇到了这个问题,想用cron 来启动php-fp ...
- 任务计划cron
在linux中,任务计划分俩:未来时间只执行一次和周期性执行 at:未来时间只执行一次 -V 显示版本信息 -l: 列出指定队列中等待运行的作业:== atq -d: 删除指定的作业:== atrm ...
- 查找文件与cron计划任务
查找文件 • 根据预设的条件递归查找对应的文件 find [目录] [条件1] [-a|-o] [条件2] ... -type 类型(f文件.d目录.l快捷方式) -name "文档名称 ...
随机推荐
- css之文本两端对齐的两种解决方法
说起文本对齐,大家都知道text-align,最常用的有left.right.center,今天我们说一下justify,也就是文本两端 对齐.说起来简单,但是有些小坑大家还是要注意的. 现在我们有这 ...
- ant 自动化构建环境。
ant 一个自动化构建的工具 可以帮你实现自动化打包和发布到web环境上. 项目目录如下: 项目一:common-framework 项目二:estun-platform-web 其中项目一是项目二 ...
- 什么是WCF
WCF的全称是windows的通信基础 Windows Communication Foundation.本质上来讲,他是一套软件开发包. 服务终节点的三要素 用来发送接收消息的构造 创建一个WCF程 ...
- PAT(甲级)2019年春季考试
7-1 Sexy Primes 判断素数 一个点没过17/20分 错因:输出i-6写成了输出i,当时写的很乱,可以参考其他人的写法 #include<bits/stdc++.h> usin ...
- PAT乙级练习(1001)
1001 害死人不偿命的(3n+1)猜想 卡拉兹(Callatz)猜想: 对任何一个正整数 n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把 (3n + 1) 砍掉一半.这样一直反复砍下去,最 ...
- 纯CSS背景噪声.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 华为云 AI 实战营计划,带你迈上 AI 之路
当今,AI的开发人才需求呈现极大的供需不平衡.所有开发者都关心,要如何从一名开发者晋升为AI开发者?AI开发能力,是主要的进入障碍.不用慌,华为云推出了 <华为云ModelArts-Lab AI ...
- NB-IoT将成为未来5G物联网主流技术
日前,我国完成了IMT-2020(5G)候选技术方案的完整提交.据悉,在提交的方案中,NB-IoT技术被正式纳入5G候选技术集合,预计2020年6月ITU将正式宣布5G技术方案的诞生.而NB-IoT也 ...
- where 和having 的区别
where : 约束声明,在查询结果返回之前对数据库中的查询条件进行约束 其后不能写聚合函数 having 过滤声明,在查询结果返回之后进行过滤,
- springboot整合thymleaf模板引擎
thymeleaf作为springboot官方推荐使用的模板引擎,简单易上手,功能强大,thymeleaf的功能和jsp有许多相似之处,两者都属于服务器端渲染技术,但thymeleaf比jsp的功能更 ...