当前位置:网站首页>crontab timing operation
crontab timing operation
2022-07-31 06:32:00 【xp_fangfei】
crontab 介绍
Linux crontab 是用来定期执行程序的命令.
当安装完成操作系统之后,默认便会启动此任务调度命令.
crond 命令每分钟会定期检查是否有要执行的工作,如果有要执行的工作便会自动执行该工作.
注意:新创建的 cron 任务,不会马上执行,至少要过 2 分钟后才可以,当然你可以重启 cron 来马上执行.
而 linux 任务调度的工作主要分为以下两类:
- 系统执行的工作:系统周期性所要执行的工作,如备份系统数据、清理缓存
- 个人执行的工作:某个用户定期要做的工作,例如每隔 10 分钟检查邮件服务器是否有新信,这些工作可由每个用户自行设置
crontab 格式说明

在以上各个字段中,还可以使用以下特殊字符:
- "* "代表所有的取值范围内的数字,如月份字段为*,则表示1到12个月;
- "/"代表每一定时间间隔的意思,如分钟字段为*/10,表示每10分钟执行1次.
- "-"代表从某个区间范围,是闭区间.如“2-5”表示“2,3,4,5”,小时字段中0-23/2表示在0~23点范围内每2个小时执行一次.
- ","分散的数字(不一定连续),如1,2,3,4,7,9.
注:由于各个地方每周第一天不一样,因此Sunday=0(第一天)或Sunday=7(最后1天).
crontab 语法
crontab [ -u user ] file
或
crontab [ -u user ] {
-l | -r | -e }
参数说明:
-u user 是指设定指定 user 的时程表,这个前提是你必须要有其权限(比如说是 root)才能够指定他人的时程表.如果不使用 -u user 的话,就是表示设定自己的时程表.
-e : 执行文字编辑器来设定时程表,内定的文字编辑器是 VI,如果你想用别的文字编辑器,则请先设定 VISUAL 环境变数来指定使用那个文字编辑器(比如说 setenv VISUAL joe)
-r : 删除目前的时程表
-l : 列出目前的时程表
实例1
//每一分钟执行一次 /bin/ls:
* * * * * /bin/ls
//在 12 月内, 每天的早上 6 点到 12 点,每隔 3 个小时 0 分钟执行一次 /usr/bin/backup:
0 6-12/3 * 12 * /usr/bin/backup
//周一到周五每天下午 5:00 寄一封信给 [email protected]:
0 17 * * 1-5 mail -s "hi" [email protected].name < /tmp/maildata
实际配置
Take your own computer as an example:
crontab -e
结果:
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
Write the operation you want to perform at the end of the file
54 21 * * * rm -r /home/ubuntu/log #每天晚上九点54删除日志
注意:The editor is selected when editing for the first time,根据自己的情况选择即可
实例2
1.删除文件命令:
find 对应目录 -mtime +天数 -name "文件名" -exec rm -rf {
} \;
实例命令:
find /export/Logs/ -mtime +2 -name "*.log" -exec rm -rf {
} \;
#将/export/Logs/目录下所有2天前带".log"的文件全部删除.
find /export/log/ -mtime +2 -name "*.20*.log" -exec rm -rf {
} \;
#将/export/Logs/目录下所有2天前带*.20*.log的文件全部删除.
具体参数说明如下:
- find:linux的查找命令,用户查找指定条件的文件;
- /export/Logs/:想要进行清理的任意目录;
- -mtime:标准语句写法;
- +2:查找2天前的文件,这里用数字代表天数;
- “.log"、".20*.log”:希望查找的数据类型,".log"表示查找扩展名为log的所有文件,""表示查找所有文件,以此类推,灵活使用
- -exec:固定写法;
- rm -rf:强制删除文件,包括目录;
- {} \ ; :固定写法,一对大括号+空格+\ ;
边栏推荐
- ImportError: cannot import name ‘Xxxx‘ from partially initialized module ‘xx.xx.xx‘
- 化学试剂磷脂-聚乙二醇-氨基,DSPE-PEG-amine,CAS:474922-26-4
- 禅道安装及使用教程
- Web Screenshots and Reverse Proxy
- 应用usb_cam同时打开多个摄像头方法
- Embedding前沿了解
- IDEA控制台不能输入信息的解决方法
- Tensorflow steps on the pit while using it
- wangeditor编辑器内容传至后台服务器存储
- Tensorflow——演示
猜你喜欢

np.fliplr与np.flipud

After unicloud is released, the applet prompts that the connection to the local debugging service failed. Please check whether the client and the host are under the same local area network.

2021年软件测试面试题大全

WeChat applet source code acquisition and decompilation method

qt:cannot open C:\Users\XX\AppData\Local\Temp\main.obj.15576.16.jom for write

Cholesterol-PEG-Thiol CLS-PEG-SH 胆固醇-聚乙二醇-巯基

科研试剂Cholesterol-PEG-Maleimide,CLS-PEG-MAL,胆固醇-聚乙二醇-马来酰亚胺

Cholesterol-PEG-Azide CLS-PEG-N3 Cholesterol-PEG-Azide MW:3400

Tensorflow steps on the pit while using it

The browser looks for events bound or listened to by js
随机推荐
虚拟机查看端口号进程
超参数优化-摘抄
【解决问题】RuntimeError: The size of tensor a (80) must match the size of tensor b (56) at non-singleton
2021年软件测试面试题大全
When solving background-size:cover, the picture is covered but not displayed completely?
二进制转换成十六进制、位运算、结构体
朴素贝叶斯文本分类(代码实现)
自己设置的私密文件,在哪找
Tensorflow——demo
mPEG-DMPE 甲氧基-聚乙二醇-双肉豆蔻磷脂酰乙醇胺用于形成隐形脂质体
cocos2d-x implements cross-platform directory traversal
qt:cannot open C:\Users\XX\AppData\Local\Temp\main.obj.15576.16.jom for write
VS2017 connects to MYSQL
Nmap的下载与安装
钉钉H5微应用免登鉴权
Podspec verification dependency error problem pod lib lint , need to specify the source
Learn how to get a database connection with JDBC
科学研究用磷脂-聚乙二醇-活性酯 DSPE-PEG-NHS CAS:1445723-73-8
VS connects to MYSQL through ODBC (1)
Wangeditor rich text editor to upload pictures and solve cross-domain problems