当前位置:网站首页>crontab定时执行任务
crontab定时执行任务
2022-06-09 17:54:00 【edycm】
crontab
按照配置定时执行任务
参数说明
Usage:
crontab [options] file
crontab [options]
crontab -n [hostname]
Options:
-u <user> define user # 申明crontab user,对该用户的定时任务进行操作
-e edit user's crontab # 执行编辑器来设置定时任务,编辑器默认为VI -l list user's crontab # 查看定时任务
-r delete user's crontab # 删除定时任务 -i prompt before deleting # 删除前提示 -n <host> set host in cluster to run users' crontabs
-c get host in cluster to run users' crontabs
-s selinux context
-x <mask> enable debugging
定时任务格式
* * * * * <cmd>
| | | | | 任务的执行脚本
| | | | 星期中的星期几(0-6)
| | | 月份(1-12)
| | 日期(1-31)
| 小时(0-23)
分钟(0-59)
注:
* :表示都要执行,如:分钟为*,则表示每分钟都要执行
a-b : 表示a-b范围都要执行,如: 分钟为1-10,表示第1分钟到第10分钟这段时间内每隔1分钟都要执行
*/n : 表示每隔n执行一次,如:分钟为*/3,表示每隔三分钟执行一次
a,b,c : 表示a,b,c时需执行一次,如:分钟为1,10,20,表示在第1分钟、第10分钟、第20分钟时都要执行
使用示例
每分钟输出测试日志到/tmp/1.log:
* * * * * echo `date -u +\%Y-\%m-\%d\ \%H:\%M:\%S.\%N` test log `uuidgen` >>/tmp/1.log
0-23中每两小时0分执行一次cmd
0 0-23/2 * * * <cmd>
周一到周五的5点执行一次cmd
0 17 * * 1-5 <cmd>
注意事项
每次执行定时任务后系统都会发送一条邮件给当前用户,如果不想收到可以在脚本后边加上重定向
>/dev/null 2>&1,调试的话可以开启,定时任务执行出错时会在邮件中显示,也会显示脚本打印的内容,邮件在/var/spool/mail/root中,可以在/var/log/cron中看到定时执行的命令邮件示例:
From [email protected] Wed May 25 05:26:01 2022 Return-Path: <[email protected]> X-Original-To: root Delivered-To: [email protected] Received: by localhost.localdomain (Postfix, from userid 0) id 9A035B63; Wed, 25 May 2022 05:26:01 +0800 (CST) From: "(Cron Daemon)" <[email protected]> To: [email protected] Subject: Cron <[email protected]> /bin/sh -c "echo `date -u +%Y-%m-%d\ %H:%M:%S.%N` test log `uuidgen`" Content-Type: text/plain; charset=UTF-8 Auto-Submitted: auto-generated Precedence: bulk X-Cron-Env: <XDG_SESSION_ID=84> X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0> X-Cron-Env: <LANG=en_US.UTF-8> X-Cron-Env: <SHELL=/bin/sh> X-Cron-Env: <HOME=/root> X-Cron-Env: <PATH=/usr/bin:/bin> X-Cron-Env: <LOGNAME=root> X-Cron-Env: <USER=root> Message-Id: <[email protected]> Date: Wed, 25 May 2022 05:26:01 +0800 (CST) 2022-05-24 21:26:01.617994410 test log 636293fd-e4bd-4f21-b8fb-f203e68fb0e0在crontab定时任务中,日期需写成
date +\%Y-\%m-\%d\ \%H:\%M:\%S.\%N,不能写成date +"%Y-%m-%d %H:%M:%S.%N"写成后者会报错:
/bin/sh: -c: line 0: unexpected EOF while looking for matching `"' /bin/sh: -c: line 1: syntax error: unexpected end of file定时任务中的脚本需写绝对路径,脚本中如果用到了环境变量,需调试确认是否有值
边栏推荐
- 如何以案例学习kd树构建和搜索过程?
- Influence of K value selection in KNN on Model
- NLP keyword extraction overview
- C language to solve the problem of climbing stairs
- 图片搜索在 Dropbox 中的工作原理
- Arm instruction set review | basic instruction usage
- Solve the chrome prompt: "your connection is not a private connection" and there is no continue access option
- CNN - nn.Conv1d使用
- build sqllite from amalgamation version
- Abbexa 质粒 MiniPrep 试剂盒检测程序
猜你喜欢

刷脸认证如何实现人脸又快又准完成校验?

NLP text representation word bag model and TF-IDF

redis源码学习-01_Clion中调试redis源码

AI首席架构师3-AICA-智慧城市中的AI应用实践

NLP-RNN

ZigBee networking has never been so simple!

NLP- 关键词提取 - 综述
![[long time series prediction] time series decomposition of [2] model components for detailed explanation of aotoformer code](/img/27/132ff1e68e9e34a9d50f1a5c3f4a2d.png)
[long time series prediction] time series decomposition of [2] model components for detailed explanation of aotoformer code

充电桩的B面是......不只公众号?还有智充小程序!

Running the code, I want to add a progress bar to see the running progress of the following code in real time. How can I break it?
随机推荐
[notes of advanced mathematics] Green formula, Gauss formula, Stokes formula, field theory
【高等数学笔记】格林公式、高斯公式、斯托克斯公式、场论
How to realize face verification quickly and accurately?
Android caching mechanism lrucache
NLP-RNN
NLP text representation word bag model and TF-IDF
Welcome to the InfoQ writing platform!
NLP - Keyword Extraction - textrank
Epigentek BTAF1多克隆抗体说明书
Abbexa 质粒 MiniPrep 试剂盒检测程序
Application of die cutting products in different fields
About concurrency and parallelism, are the fathers of go and Erlang wrong?
Epigentek btaf1 polyclonal antibody instructions
Application of slip ring in automatic control system
如何实现自定义富文本编辑器标签
演练-分组和聚合函数的组合使用
Epigentek hi fi cDNA synthesis kit instructions
[data processing] pandas reads SQL data
Some interesting b+ tree optimization experiments
slurm program running without multiprocess instead run individually