crond 任务调度
service crond restart //重启任务调度
1. crontab 进行定时任务的设置
1. 任务调度:
是指系统在某个时间执行的特定的命令或程序。
2. 任务调度分类:
1)系统工作:有些重要的工作必须周而复始地执行。如病毒扫描等
2)个别用户工作:个别用户可能希望执行某些程序,比如对mysql数据库的备份。
3. 基本语法 crontab [选项]
常用选项
1)-e 编辑crontab定时任务
如:*/1 * * * * ls -1 /etc/ > /tmp/to.txt
意思说每小时的每分钟执行ls -1 /etc/ > /tmp/to.txt命令
[root@localhost tmp]# crontab -e
crontab: installing new crontab
[root@localhost tmp]# cat to.txt
2)-l 查询crontab任务
[root@localhost tmp]# crontab -l
*/1 * * * * ls -1 /etc/ > /tmp/to.txt
3)-r 删除当前用户的所有crontab任务
[root@localhost tmp]# crontab -r
*/1 * * * * ls -1 /etc/ > /tmp/to.txt
参数细节说明5个占位符的说明
第一个 一小时当中的第几分钟 0-59
第二个 一天当中的第几小时 0-23
第三个 "*” 一个月当中的第几天 1-31
第四个 * 一年当中的第几月 1-12
第五个 一周当中的星期几 0-7(0和7都代表星期日)
3)出现问题:
[root@localhost logonuser]# crontab -e
crontab: installing new crontab
"/tmp/crontab.tRa0Yr":1: bad minute
errors in crontab file, can't install.
Do you want to retry the same edit?
如果在执行命令的时候出现该问题时,检查参数之间是否有空格,*号数目是否正确(5个)
4. 特殊符号的说明
5. 应用案例
案例1:每隔一分钟,就将当前的日期追加到/temp/mydate文件
[root@localhost logonuser]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@localhost logonuser]# date
2022年 04月 11日 星期一 10:24:59 CST
[root@localhost logonuser]# ll
总用量 40
-rwxr-xr-x. 1 root root 15 4月 6 15:28 banana.txt
-rwxrwxrwx. 1 logonuser logonuser 1204 4月 2 12:14 happy.txt
-rw-r--r--. 1 root root 196 4月 1 14:43 info.txt
drwxrwxrwx. 3 logonuser logonuser 4096 4月 11 10:25 learn
-rw-r--r--. 1 root root 340 4月 2 15:35 learn.zip
-rw-r--r--. 1 root root 18 4月 1 16:02 mydata.txt
-rw-r--r--. 1 root root 215 4月 11 10:22 mydate
-rw-r--r--. 1 root root 223 4月 2 15:51 myinfo.tar.gz
lrwxrwxrwx. 1 root root 5 4月 6 11:22 myopt -> /opt/
-rw-r--r--. 1 root root 922 4月 2 15:53 study.tar.gz
drwxr-xr-x. 4 root root 4096 4月 6 16:10 test
[root@localhost logonuser]# cd learn/
[root@localhost learn]# ll
总用量 8
-rw-r--r--. 1 root root 43 4月 11 10:25 mydtate
drwxr-xr-x. 2 root root 4096 4月 1 11:25 oneday
[root@localhost learn]# vim mydtate
[root@localhost learn]# vim mysh.sh
[root@localhost learn]# ll
总用量 12
-rw-r--r--. 1 root root 602 4月 11 10:38 mydtate
-rw-r--r--. 1 root root 71 4月 11 10:38 mysh.sh
drwxr-xr-x. 2 root root 4096 4月 1 11:25 oneday
[root@localhost learn]# chmod u+x mysh.sh
[root@localhost learn]# ll
总用量 12
-rw-r--r--. 1 root root 774 4月 11 10:42 mydtate
-rwxr--r--. 1 root root 71 4月 11 10:38 mysh.sh
drwxr-xr-x. 2 root root 4096 4月 1 11:25 oneday
[root@localhost learn]# crontab -e
[root@localhost learn]# date
2022年 04月 11日 星期一 10:54:49 CST
[root@localhost learn]# ll
总用量 12
-rw-r--r--. 1 root root 1290 4月 11 10:54 mydtate
-rwxr--r--. 1 root root 71 4月 11 10:38 mysh.sh
drwxr-xr-x. 2 root root 4096 4月 1 11:25 oneday
[root@localhost learn]# date
2022年 04月 11日 星期一 10:54:59 CST
[root@localhost learn]# ll
总用量 16
-rw-r--r--. 1 root root 200 4月 11 10:55 mycal
-rw-r--r--. 1 root root 1333 4月 11 10:55 mydtate
-rwxr--r--. 1 root root 71 4月 11 10:38 mysh.sh
drwxr-xr-x. 2 root root 4096 4月 1 11:25 oneday
[root@localhost learn]# vim mycal
[root@localhost learn]# date
2022年 04月 11日 星期一 11:10:37 CST
[root@localhost learn]# vim mycal
案例3:每天凌晨两点,将MySQL数据库testdb,备份到文件中
指令为 mysqldump-u root -p密码 数据库 > /home/db.bak
(1)crontab -e
(2)0 2 * * * mysqldump-u root -proot testdb > /home/db.bak
ldump-u root -proot testdb > /home/db.bak