Cron: Run every day except first day of the month and first day of each week

Ian Newson

I'm fairly new to cron and I'm trying to figure out a way to run a job every day of the week, except for the first day of each month and the first day of each week.

This may sound a little odd, but the context is that this is for a backup task. I want to backup every day, but also keep some backups around a little longer so take one backup each week and also one each month.

I want to setup a cron task for the daily job but I'm struggling to see how to effectively not run the daily task if it's on a day on which the weekly or monthly backup will also run. I think I can achieve this by adding a separate cron task for each day of the month, but that feels wrong because it's a lot of separate tasks.

Is there a better way to achieve this in cron?

Jeff Schaller

I would schedule the job with a day-of-the-week restriction then prefix the actual job with a date test for "day of the month". Use your own values for the hour & minute fields. The "day-of-the-month" field is *, meaning run on any & all days of the month. The "month" field is also open. The "day of the week" field is restricted to Mondays through Saturdays, skipping the first day of the week (assuming you count Sunday as the first day of the week). If Monday is the first day of your week, use 0,2-6 as the "day of the week" value instead.

(minute) (hour) * * 1-6 [ $(date +\%e) -ne 1 ] && actual-job

The simplest approach (of restricting both the day-of-the-month and day-of-the-week fields) doesn't work, due to this Note in man 5 crontab, with my bolded emphasis:

Note: The day of a command's execution can be specified in the following two fields — 'day of month', and 'day of week'. If both fields are restricted (i.e., do not contain the "*" character), the command will be run when either field matches the current time. For example,

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get the first day of the current week and month?

first day != "first day of this month"

Bigquery: Getting first day of the week/month in standard SQL

First day of week (beginning with Sunday)

Calculate first day and last day of week of current month

First and last entry of every day for each employee

Cron - How to run a job on the first weekday after a specific day of the month?

Getting first and last day of each month in R

How to get first & last day of current week/month

First and last day of a month

Cronjob for first day of month except sunday

the first day of the month

How to get first day and last day for every week in a month?

First day of the week and the calendar week

Schedule a job for the first day of each month

The first day and the last day of the month

ICalendar recurring event starts first day of month, ends last day of month, repeats every month

First Day of month

First day of every week in every month in PowerApps

First Day of the Week Postgres

ReactJS antd DatePicker enabled only every first day of each month

How to efficiently find the first timestamp of each day/week/month in an array of timestamps?

How to get first and last day in specific week in month

How to Get Day of Week as Integer with First of Month Changing Values?

How to find day of the week for each date of that month if the day of first date is given?

Add the first day of each month to year month date in python

Value of first day of current month (except zero)

C# - Get the first day and last day of the week knowing the week number, month number and year

Run Sql Job in The first day of every shamsi month (persian date)