ITeye博客

crontab 每月最后一天执行

java_2007 · 2011-09-26 14:48 · Linux · 操作系统 · Markdown

如何设定crontab在每月最后一天执行一种方法:

for Linux
0 8 28-31 * * [ `date -d tomorrow +\%e` -eq 1 ] && (shell script)

for other Unix,BSD
0 8 28-31 * * [ `echo \`cal\` | awk '{print $NF}'` -eq 1 ] && (shell script)

另一种方法:
单独靠crontab判断比较复杂,所以把判断部分写到执行脚本中

#!/bin/bash
today=`date +%d`
last_day=`cal | xargs | awk '{print $NF}'`
if [ "$today" != "$last_day" ];then   
 exit 1
fi
....  # other codes start from here

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。