在每月的第一个工作日安排任务
Schedule task on the first workday of the month
我目前有一项任务是从我的网站收集使用情况统计信息,并设置为自动将它们通过电子邮件发送给客户。
问题是 1 号可能是非工作日,我想这不是灾难,但看起来有点不专业。
这是我目前安排的方式:
$schedule
->command("report", [ "--email" => "example@example.com" ]) //My command which accepts the email as a parameter
->monthly();
我正在考虑执行以下操作:
$schedule
->command("report", [ "--email" => "example@example.com" ]) //My command which accepts the email as a parameter
->monthlyOn(1)
->when(function () {
if (in_array(Carbon::now()->dayOfWeek,[Carbon::SATURDAY,Carbon::SUNDAY])) {
return false;
}
return true;
});
$schedule
->command("report", [ "--email" => "example@example.com" ]) //My command which accepts the email as a parameter
->monthlyOn(2)
->when(function () {
if (Carbon::now()->dayOfWeek == Carbon::MONDAY) {
return true; //1st was in the weekend
}
return false;
});
$schedule
->command("report", [ "--email" => "example@example.com" ]) //My command which accepts the email as a parameter
->monthlyOn(3)
->when(function () {
if (Carbon::now()->dayOfWeek == Carbon::MONDAY) {
return true; //1st and 2nd was in the weekend
}
return false;
});
然而,对于这么简单的事情,这看起来是一件非常奇怪的事情。
所以我的问题:
- 如果
when
条件失败,是否会再次尝试任务直到成功? (假设这是一个没有但不确定)
- 是否有更简单的方法来 运行 在每个月的第一个工作日执行任务?
我会post将其作为社区维基答案供其他人在将来需要时使用:
You can put the condition into the actual crontab command:
[ "$(date '+%a')" = "Mon" ] && echo "It's Monday"
Now, if this
condition is true on one of the first seven days in a month, you have
its first Monday. Note that in the crontab, the percent-syntax needs
to be escaped though:
0 12 1-7 * * [ "$(date '+\%a')" = "Mon" ] && echo "It's Monday"
以上引用自:https://superuser.com/questions/428807/run-a-cron-job-on-the-first-monday-of-every-month
通过这种方式,它被设置为每月 运行 仅在星期一执行一次的 cronjob。我相信这将是您实现目标的最有效方法。
我知道接受的答案是正确的,但是,总有不同的方式我更喜欢这个:
->monthlyOn(Carbon::parse('first monday of this month')->format('d'), '5:00')
我目前有一项任务是从我的网站收集使用情况统计信息,并设置为自动将它们通过电子邮件发送给客户。
问题是 1 号可能是非工作日,我想这不是灾难,但看起来有点不专业。
这是我目前安排的方式:
$schedule
->command("report", [ "--email" => "example@example.com" ]) //My command which accepts the email as a parameter
->monthly();
我正在考虑执行以下操作:
$schedule
->command("report", [ "--email" => "example@example.com" ]) //My command which accepts the email as a parameter
->monthlyOn(1)
->when(function () {
if (in_array(Carbon::now()->dayOfWeek,[Carbon::SATURDAY,Carbon::SUNDAY])) {
return false;
}
return true;
});
$schedule
->command("report", [ "--email" => "example@example.com" ]) //My command which accepts the email as a parameter
->monthlyOn(2)
->when(function () {
if (Carbon::now()->dayOfWeek == Carbon::MONDAY) {
return true; //1st was in the weekend
}
return false;
});
$schedule
->command("report", [ "--email" => "example@example.com" ]) //My command which accepts the email as a parameter
->monthlyOn(3)
->when(function () {
if (Carbon::now()->dayOfWeek == Carbon::MONDAY) {
return true; //1st and 2nd was in the weekend
}
return false;
});
然而,对于这么简单的事情,这看起来是一件非常奇怪的事情。
所以我的问题:
- 如果
when
条件失败,是否会再次尝试任务直到成功? (假设这是一个没有但不确定) - 是否有更简单的方法来 运行 在每个月的第一个工作日执行任务?
我会post将其作为社区维基答案供其他人在将来需要时使用:
You can put the condition into the actual crontab command:
[ "$(date '+%a')" = "Mon" ] && echo "It's Monday"
Now, if this condition is true on one of the first seven days in a month, you have its first Monday. Note that in the crontab, the percent-syntax needs to be escaped though:
0 12 1-7 * * [ "$(date '+\%a')" = "Mon" ] && echo "It's Monday"
以上引用自:https://superuser.com/questions/428807/run-a-cron-job-on-the-first-monday-of-every-month
通过这种方式,它被设置为每月 运行 仅在星期一执行一次的 cronjob。我相信这将是您实现目标的最有效方法。
我知道接受的答案是正确的,但是,总有不同的方式我更喜欢这个:
->monthlyOn(Carbon::parse('first monday of this month')->format('d'), '5:00')