大网数据广告活动
服务器运维问答

Daily, Weekly and Monthly Cron job

· 52CE 我爱测

Recently I wrote a simple little script to backup a directory to S3. My goal is to run daily, weekly and monthly backups the have different retention rules based on the frequency in S3. Everything is working pretty well with one …

问题原文

原帖链接 · Staros · CC BY-SA 3.0

Recently I wrote a simple little script to backup a directory to S3. My goal is to run daily, weekly and monthly backups the have different retention rules based on the frequency in S3.

Everything is working pretty well with one exception. My WEEKLY cron job runs every day. Here's the crontab:

0 3 1 * * /usr/s3_sync_dir /dropbox MONTHLY
0 2 2-31 * THU /usr/s3_sync_dir /dropbox WEEKLY
0 3 2-31 * * /usr/s3_sync_dir /dropbox DAILY

So the MONTHLY job runs at 3am on the 1st of every month. The WEEKLY should then run on Thursdays at 2am if it's not the 1st. And the DAILY just runs every day that's not the 1st. This online validator confirms that the WEEKLY should just run every Thursday.

Anyone have thoughts as to why my WEEKLY job runs every morning?

This is on Ubuntu 16.04.2 LTS.

已采纳解答

原帖链接 · ychaouche · CC BY-SA 3.0

From the manual (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. One can, however, achieve the desired result by adding a test to the command (see the last example in EXAMPLE CRON FILE below).

I guess you should turn your 2-31 into *

OR

you can use @daily, @weekly and @monthly.

OR

you can link to your scripts in /etc/cron.daily, /etc/cron.weekly, /etc/cron.monthly.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-04-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。