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. 解决Debina系统自动更新软件包的问题

    不知从何时开始,我的电脑每天开机连接上网络之后,不断的在下载数据,状态栏显示网速达到每秒1到2兆.开始我还不太在意,不过后来由于带宽全部被这种莫名其奥妙的下载占据了,我连网页都无否正常浏览了,所以我决 ...

  2. adb查看Android app包名

    1. 确保电脑已经安装了adb.exe文件 2. 使用USB连接你的手机到电脑上,输入下图所示的命令后按下回车键:adb devices 3. 使用手机打开某个应用程序 4.回到命令号窗口,输入代码如 ...

  3. 【并发编程】关于Thread类的详细介绍

    多线程编程基础--Thread类 Thread类是Java中实现多线程编程的基础类.本篇博客就来介绍下Thread类的常用API和常见用法. Thread类常用的方法如下: Thread.active ...

  4. c语言博客专业05

    问题 答案 这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-2/homework/8655 我在 ...

  5. Spring Boot中使用Jpa的findOne方法不能传入id

    最近通过慕课网学习spring boot,视频中通过jpa的findOne方法以id为参数查询出对应的信息, 而当我自己做测试的时候却发现我的findOne方法的参数没有Integer类型的id,而是 ...

  6. 【java】使用java.util的【Collections】简化List创建

    我们在创建一个List并往其中加入一个元素的时候一般会这么做: public List<User> getCurrentUser() { List<User> users = ...

  7. RestSharp Simple REST and HTTP API Client for .NET

    var client = new RestClient("http://example.com"); // client.Authenticator = new HttpBasic ...

  8. 在 Linux 部署多节点 Kubernetes 集群与 KubeSphere 容器平台

    KubeSphere 是在 Kubernetes 之上构建的以应用为中心的企业级容器平台,所有供为用户提供简单易用的操作界面以及向导式操作方式.同时,KubeSphere Installer 提供了 ...

  9. 写一个umi插件 自动生成代码 解放cv的双手

    引言 最近在写一个中台项目,使用的react的umi框架. 各种增删改查.基本是列表页 新建页 详情页这种页面 为了避免不必要的简单重复(主要是想偷懒) 于是想去实现自己的一个代码生成器 探索 首先, ...

  10. Python中的Tcp协议应用之TCP服务端-线程版

    利用线程实现,一个服务端同时服务多个客户端的需求. TCP服务端-线程版代码实现: import socket import threading def handle_client_socket(ne ...