-------------------------------------资源来源于网络,仅供自学使用,如有侵权,联系我必删.

第一:

crond 任务调度原理示意图

第九节 实操篇-crond 任务调度

crontab 进行 定时任务的设置

----------------------------------------------------------------------------------------------------

第二:

概述
任务调度:是指系统在某个时间执行的特定的命令或程序。

任务调度分类:

1.系统工作:有些重要的工作必须周而复始地执行。如病毒扫描等

2.个别用户工作:个别用户可能希望执行某些程序,比如对 mysql 数据库的备份。

 

基本语法
crontab [选项]
常用选项

第九节 实操篇-crond 任务调度

快速入门
设置任务调度文件:/etc/crontab
设置个人任务调度。执行crontab –e命令。
  接着输入任务到调度文件
  如:*/1 * * * * ls –l /etc/ > /tmp/to.txt
意思说每小时的每分钟执行ls –l /etc/ > /tmp/to.txt命令
 

步骤如下
1) crontab -e

第九节 实操篇-crond 任务调度
2) */ 1 * * * * ls -l /etc >> /tmp/to.txt

第九节 实操篇-crond 任务调度
3) 当保存退出后就程序。

第九节 实操篇-crond 任务调度
4) 在每一分钟都会自动的调用 ls -l /etc >> /tmp/to.txt

第九节 实操篇-crond 任务调度

 

参数细节说明
• 5个占位符的说明

第九节 实操篇-crond 任务调度


• 特殊符号的说明

第九节 实操篇-crond 任务调度


• 特定时间执行任务案例

第九节 实操篇-crond 任务调度

 

应用实例
案例1 :每隔1 分钟,就将当前的日期信息,追加到 /tmp/mydate  文件中

1) 先编写一个文件 /home/mytask1.sh
date >> /tmp/mydate

第九节 实操篇-crond 任务调度

第九节 实操篇-crond 任务调度
2) 给 mytask1.sh 一个可执行权限
chmod 744 /home/mytask1.sh

第九节 实操篇-crond 任务调度
3) crontab -e

第九节 实操篇-crond 任务调度
4) */1 * * * * /home/mytask1.sh

第九节 实操篇-crond 任务调度

第九节 实操篇-crond 任务调度
5) 成功

在tmp目录下查看

第九节 实操篇-crond 任务调度

 

案例2 :每隔1 分钟, 到 将当前日期和日历都追加到 /home/mycal  文件中

1) 先编写一个文件 /home/mytask2.sh
  date >> /tmp/mycal
  cal >> /tmp/mycal

第九节 实操篇-crond 任务调度
2) 给 mytask2.sh 一个可执行权限
  chmod 744 /home/mytask2.sh

第九节 实操篇-crond 任务调度
3) crontab -e

第九节 实操篇-crond 任务调度
4) */1 * * * * /home/mytask2.sh

第九节 实操篇-crond 任务调度
5) 成功

在tmp目录下查看

第九节 实操篇-crond 任务调度

 

案例3: 每天凌晨2:00  将mysql 数据库 testdb  ,备份到文件中。

1) 先编写一个文件 /home/mytask3.sh
/usr/local/mysql/bin/mysqldump -u root -proot testdb > /tmp/mydb.bak

第九节 实操篇-crond 任务调度

第九节 实操篇-crond 任务调度
2) 给 mytask3.sh 一个可以执行权限

chmod 744 /home/mytask3.sh

第九节 实操篇-crond 任务调度
3) crontab -e

第九节 实操篇-crond 任务调度
4) 0 2 * * * /home/mytask3.sh

第九节 实操篇-crond 任务调度
5) 成功

第九节 实操篇-crond 任务调度

 

 

crond  相关指令:
1) crontab –r:终止任务调度。

第九节 实操篇-crond 任务调度

第九节 实操篇-crond 任务调度
2) crontab –l:列出当前有那些任务调度

第九节 实操篇-crond 任务调度
3) service crond restart [重启任务调度]

第九节 实操篇-crond 任务调度

相关文章: