special cron expression: how to make an exception?

2019-08-13 04:26发布

I made a cron to execute a task every tuesday at 3:50 AM - except a tuesday which coincides with the first day of the month:

50 3 * * 2 MyCommand

but I don't know how I can translate my exception into the cron syntax, any tips?

标签: linux cron
2条回答
劳资没心,怎么记你
2楼-- · 2019-08-13 04:48

You can set the "day of month" field to the range 2-31, effectively excluding the first day. This should do it:

50 3 2-31 * 2 MyCommand
查看更多
家丑人穷心不美
3楼-- · 2019-08-13 04:51

Can you put a conditional in your script? I would do that. And in your cron you can comment that it won't run on the first per your script directions

As an example, in bash you can do this:

#!/bin/bash

dayofmonth=`date +"%d"`
if [ $dayofmonth == "01" ];
then
# do not run, exit
exit
fi

# otherwise go on

echo "it is not the first"

So your cron would be

30 5 * * 2 /path/to/script # comment: script conditional in place to not run on the 1st
查看更多
登录 后发表回答