关于crontab里day和weekday的使用
来源:互联网 发布:淘宝兼职平面模特 编辑:程序博客网 时间:2024/06/05 01:05
Crontab 每兩週執行一次
今天被問到一個問題: Crontab 如何設定兩週執行一次.
- 問題假設: 每個月 "第 1, 3 週" 的 "星期一 早上6點" 要執行 "ls /tmp" 的指令.
原本想想應該是直接設定 0 6 1-7,15-21 * 1
就可以了, 結果 1-7, 15-21 和 星期一也都會跑.
- man 5 crontab # 找到下述解釋
Note: The day of a command's execution can be specified by two fields -- day of month, and day of week. If both fields are restricted (i.e., aren't *), the command will be run when either field matches the current time.
For example,
``30 4 1,15 * 5'' would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.
註: weekday 和 day 這兩欄很容易造成混淆, 假如兩欄同時都被指定時, 只需滿足其中一欄就算符合.
目前想到的解法, 就是在程式判斷, 不然就是在 Crontab 設定時判斷, 找了很多資料, 還沒找到正確解法. (若有知道解法的, 請不吝指教.. Orz.)
解法
- Crontab 中設定: 0 6 1-7,15-21 * * if [ `date '+\%w'` = "1" ]; then ls /tmp;fi
- 註1: bash 裡面直接用
if [ `date '+%w'` = "1" ]; then ls /tmp;fi
即可, 但是在 Crontab 中, "%" 是特殊字元, 要加上跳脫字元(escape character). - 註2: "date '+%w'" => 用數字顯示星期幾 (0~6 = 星期天~ 星期六)
- 註3: "ls /tmp" 換成想要執行的指令即可.
相關解法
- 找到解法看起來比較像, 但是看不懂是怎麼設定的: Crontab Setting Ineffective
- 上述採用這類的解法: biweekly schedule through cron - dBforums
相關網頁
- Cron 詳細寫法: Crontab 的寫法(@reboot, @yearly...)
0 0
- 关于crontab里day和weekday的使用
- 关于星期几的枚举WeekDay
- AIX 里的crontab如何使用
- 关于crontab的使用详解
- 编写一个关于星期几的枚举WeekDay
- 请编写一个关于星期几的枚举WeekDay
- 编写一个关于星期几的枚举WeekDay
- crontab的安装和使用
- aix 里的 crontab
- crontab里php使用相对路径的方法
- crontab里php使用相对路径的方法
- 关于crond和crontab
- 关于crontab和crond
- 关于datepart计算weekday时多一天引起的问题及解决方法
- Linux crontab的解读和使用
- 关于nmap和traceroute在虚拟机里使用的问题
- 关于crontab的坑
- crontab安装和使用
- 业界资讯:用as3 写后台服务?
- STL中用ERASE()方法遍历删除元素
- 高级处理机制
- 【破解EXE】VC程序的破解及修改一例
- Linux文件查找命令详解-which whereis find locate
- 关于crontab里day和weekday的使用
- 沸腾女孩广州一样出名,获邻居力赞
- Linux下crontab的使用
- Mac显示隐藏文件,找到bin,urs文件夹
- 启动 open office服务 命令
- android -- 小问题 Android WebView缓存分析
- rhel6.4网络安装kvm虚拟机
- 卡尔曼滤波的原理说明
- 《编写高质量代码:改善Java程序的151个建议》 建议13