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. 一个null,差点把系统给弄崩溃了

    今天生产上面发现了一个奇异的bug,URL上面会带上一个ID,这个ID是关联别的系统的,类似这种格式 xxx.xxx.xxx.xxx  ,是别的系统自己填写的,我们的URL会带上id=xxx.xxx. ...

  2. 学习spring和spring mvc过程中遇到的一些问题

    以下仅为个人通过网上查找资料总结的内容,如有不对的地方欢迎提出: 注解: @Controller//表明本类是一个Controller @RequestMapping(value="/dem ...

  3. android 点击无效验证

    背景 在写一个东西滑动删除列表的时候,出现了一个问题.我的需求是,左滑然后出现delete,然后点击delete,让该滑块消失. 我在点列表的第一行的时候,左滑,出现delete,点击删除,ok的,完 ...

  4. luogu P1356 数列的整数性 |动态规划

    题目描述 对于任意一个整数数列,我们可以在每两个整数中间任意放一个符号'+'或'-',这样就可以构成一个表达式,也就可以计算出表达式的值.比如,现在有一个整数数列:17,5,-2,-15,那么就可以构 ...

  5. Java中替换字符串中特定字符,replaceAll,replace,replaceFirst的区别

    使用“;”替换过字符串中的“,” public class Test01 {public static void main(String[] args) {String number = " ...

  6. word2vec用到的c语言知识

    argc,avgv用法 argc 表示有几个参数,因为至少要指定一个应用程序的名,所以argc>=1. argv表示参数是什么. int main(int argc, char **argv) ...

  7. Redis第二讲【Redis基本命令和五大数据结构】

    [二.Redis基本命令和五大数据结构] redis的基础知识和命令 redis 是一个单进程(包装epoll函数来对读写事件进行相应) 默认有16个数据库,初始使用的数据库为0号库 默认端口为637 ...

  8. Golang 怎么给WaitGroup加超时时间

    怎么给WaitGroup加超时时间呢?刚好群里有人问了我这个问题,我就把我的方法在这边贴出来了. var w = sync.WaitGroup{} var ch = make(chan bool) w ...

  9. Orleans 初接触

    简介 这篇随笔主要记录了自己学习Orleans的经过和理解,在学习过程中会一直更新,思路和理解可能有些偏颇,如果有幸有大佬看到这篇文章,希望能给予批评指正. 导航 (一) 入门例子 (二) 测试用例 ...

  10. java虚拟机栈 相关操作

    针对JVM虚拟栈 和栈帧的操作 虚拟机栈: 栈元素是栈帧.方法调用,栈帧入栈,反之出栈. 栈帧:一个方法的运行空间. 1.局部变量表:方法定义的局部变量.方法的参数存在该表. 实例方法中有个隐含参数“ ...