Crontab每月定时任务的几个demo
发布于 分类 Linux
6天前 有1个用户阅读过
# crontab定时任务
# 分 时 日 月 星期 要执行的命令
# 每个月第一天
0 0 1 * * (shell script)
# 每个月最后一天
0 0 28-31 * * [ `date -d tomorrow +\%e` -eq 1 ] && (shell script)
# 每个月第一个星期6
0 0 1-7 * 6 (shell script)
-- The End --
Commands are executed by cron when the minute, hour, and month of year fields match the current time, and when at least one of the two day fields (day of month, or day of week) match the current time.
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.
Hence you need to set up your cron job to run on every one of those days and immediately exit if it's not Saturday.
I found a solution from
[https://stackoverflow.com/questions/11683387/run-every-2nd-and-4th-saturday-of-the-month/26425901]
30 4 1,15 * * [ `date +\%u` = 5 ] && (shell script)