Linux知识点(八)----crontab定时任务

本文详细介绍了Linux系统中的crond服务及其用于定时任务的crontab命令。内容涵盖crontab的基本语法、选项用法,以及如何设置定时任务,包括系统工作和个别用户工作。通过实例演示了每分钟执行ls命令并将结果写入文件的操作,并解决可能出现的错误。此外,还展示了如何备份MySQL数据库的crontab任务设置。
摘要由CSDN通过智能技术生成

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EOPG

你的鼓励是我创造的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值