linux添加计划任务
crond 是linux用来定期执行程序的命令。当安装完成操作系统之后,默认便会启动此任务调度命令。crond命令每分锺会定期检查是否有要执行的工作,如果有要执行的工作便会自动执行该工作。可以用以下的方法启动、关闭这个服务:
[root@host ~]# /sbin/service crond start 启动
[root@host ~]# /sbin/service crond stop 关闭
Stopping crond: [ OK ]
[root@host ~]# /sbin/service crond start 启动
Starting crond: [ OK ]
[root@host ~]# /sbin/service crond restart 重新启动
Stopping crond: [ OK ]
Starting crond: [ OK ]
[root@host ~]# /sbin/service crond reload 重新载入配置
Reloading crond: [ OK ]
Linux下的任务调度分为两类,系统任务调度和用户任务调度。
系统任务调度:系统周期性所要执行的工作,比如写缓存数据到硬盘、日志清理等。
在/etc目录下有一个crontab文件,这个就是系统任务调度的配置文件。
[root@host ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# For details see man 4 crontabs
# For details see man 4 crontabs
# 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
# | | | | |
# * * * * * user-name command to be executed
第一行SHELL变量指定了系统要使用哪个shell,这里是bash
第二行PATH变量指定了系统执行 命令的路径
第三行MAILTO变量指定了crond的任务执行信息将通过电子邮件发送给root用户,如果MAILTO变量的值为空,则表示不发送任务 执行信息给用户
第四行的HOME变量指定了在执行命令或者脚本时使用的主目录。
用户任务调度:用户定期要执行的工作,比如用户数据备份等。用户可以使用 crontab 工具来定制自己的计划任务。
所有用户定义的crontab 文件都被保存在 /var/spool/cron目录中。其文件名与用户名一致。
crontab命令选项:
cron服务提供crontab命令来设定cron服务的,以下是这个命令的一些参数与说明:
crontab -u //设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数
crontab -l //列出某个用户cron服务的详细内容
crontab -r //删除没个用户的cron服务
crontab -e //编辑某个用户的cron服务
比如说root查看自己的cron设置:crontab -u root -l
再例如,root想删除fred的cron设置:crontab -u fred -r
在编辑cron服务时,编辑的内容有一些格式和约定,输入:crontab -u root -e
cron文件语法
分 小时 日 月 星期 命令
0-59 0-23 1-31 1-12 0-6 command
特殊符号的含义:
"*"代表取值范围内的数字
"/"代表"每"
"-"代表从某个数字到某个数字
","分开几个离散的数字
举例说明
5 * * * * ls //指定每小时的第5分钟执行一次ls命令
30 7 8 * * ls //每月8号的7:30分执行ls命令
30 6 * * 0 ls //每周日的6:30分执行ls
30 3 10,20 * * ls //每月10,20号的3:30分执行ls
10 1-5 * * * ls //每天1到5点,每隔10分钟执行ls
*/5 * * * * ls //每5分钟执行一次ls
30 6 */10 * * ls //每个月每隔10天6:30分执行一次ls
数据备份计划任务
创建shell脚本如下:
[root@host ~]# cat /root/shellfile/dbbak.sh
mysqldump -u root -proot test>/root/dbbak/test_`date +%Y%m%d%H%M`.sql
修改文件的可执行权限
[root@host ~]# chmod a+x /root/shellfile/dbbak.sh
新增调度任务
[root@host shellfile]# crontab -e
no crontab for root - using an empty one
*/5 * * * * /root/shellfile/dbbak.sh
查看执行情况
[root@host dbbak]# ls -al |grep 201805
-rw-r--r-- 1 root root 8154 May 29 16:55 test_201805291655.sql
[root@host dbbak]# ls -al |grep 201805
-rw-r--r-- 1 root root 8154 May 29 16:55 test_201805291655.sql
-rw-r--r-- 1 root root 8154 May 29 17:00 test_201805291700.sql
-rw-r--r-- 1 root root 8154 May 29 17:05 test_201805291705.sql
linux添加计划任务的更多相关文章
- Linux系统下使用crontab添加计划任务的方法
在服务器中添加定期执行的任务,在很多情况下是非常必要的.比如,每天清理一次/tmp目录下的文件;没几分钟检查某一守护进程是否正常等等.这样计划任务就显得尤为方便.下面将介绍如何在Linux系统中 ...
- Linux中计划任务执行脚本crontab-简洁版
我使用的是ubuntu16,所以在ubuntu中一切正常,在其他linux系统中应该都差不多. 1 计划任务,crontab命令选项: -u指定一个用户, -l列出某个用户的任务计划, ...
- Linux定时任务计划
Linux定时任务计划 在使用Linux系统时,我们有时会需要让系统在某个时间去执行特定的任务,这时就需要去了解Linux提供的定时任务功能 种类 Linux的定时任务分为两种:单一型和循环型 单一型 ...
- linux任务计划 chkconfig工具 systemd管理服务 unit介绍 target介绍
linux任务计划 任务计划:特定时间备份数据,重启服务,shell脚本,单独的命令等等. 任务计划配置文件:cat /etc/crontab [root@centos7 ~]# cat /etc/c ...
- linux中计划任务执行脚本
我使用的是ubuntu14.4,所以在ubuntu中一切正常,在其他linux系统中应该都差不多. 1 计划任务,crontab命令选项: -u指定一个用户, -l列出某个用户的任务计 ...
- linux任务计划cron
linux任务计划cron 1.crontab命令任务计划配置文件 [root@bogon ~]# cat /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/ ...
- linux任务计划cron、chkconfig工具、systemd管理服务、unit和target介绍
第8周第1次课(5月14日) 课程内容: 10.23 linux任务计划cron10.24 chkconfig工具10.25 systemd管理服务10.26 unit介绍10.27 target介绍 ...
- Linux操作系统的计划任务
Linux操作系统的计划任务 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.任务计划概述 Linux任务计划.周期性任务执行 未来的某时间点执行一次任务: at: 指定时间点, ...
- Linux任务计划at
Linux任务计划at 一Linux任务计划介绍 Linux任务计划.周期性任务执行at:未来的某时间点执行一次任务batch:系统自行选择空闲时间去执行此处指定的任务cron:周期性运行某任务 二a ...
随机推荐
- CTF之当铺密码
当铺密码即:汉字的笔画有几笔出头,则代表数字几 例如:由=1 王=6 大=5
- CSI-MIPI学习笔记
一.mipi LCD 的CLK时钟频率与显示分辨率及帧率的关系 公式: Mipiclock = [ (width+hsync+hfp+hbp) x (height+vsync+vfp+vbp) ] x ...
- day25 python学习 继承,钻石继承
通过一个列子认识父类和子类中,子类的如何实现对父类默认属性调用,同时拥有自己的属性,如何在子类中调用父类的方法,class Ainmal: country='afdas' def __init__(s ...
- 两个有用Oracle运算:intersect和minus运算
intersect运算 返回查询结果中相同的部分 exp:各个部门中有哪些相同的工种 select job from account intersect select job from researc ...
- Linux下的Nginx、php、mysql、apache部署
待补充,先搞几个博客链接: https://www.cnblogs.com/Candies/p/8282934.html http://sujianjob.com/2017/12/18/yum%E5% ...
- Apache+modjk布置tomcat集群
一.版本: Apache: 2.2.14: 下载地址:http://archive.apache.org/dist/httpd/binaries/win32/ Mod_jk:tomcat-connec ...
- es6比es5节省代码的地方总结
对象方法简写: es5写法: var obj = { name: 'jeff', getName: function () { return this.name; } } es6写法(方法定义里,少写 ...
- java 中一些需要注意的知识点
java数组的length属性是容量,而不是数组真实元素的个数: 多线程中的interrupt()方法并不会终止处于"运行状态"的线程,它只是将线程的中断标记设为true. juc ...
- IE 10 如何设置支持CRM4 正常浏览
通过工具—> 选择兼容性视图 就可以了.具体如下图:
- ios之runloop笔记
网上关于runloop的文章不计其数,再此,贴个自认为讲的比较简单明了的文章 http://www.jianshu.com/p/536184bfd163 个人理解: ios的runloop应该是类似于 ...