r.sendecky
r.sendecky

Reputation: 10373

Systemd timer for the first business day of every month

Just wondering if there is a way to schedule a timer for the first business day of every month? Basically the first day of every month which is not a weekend. I am trying to avoid creating a wrapper script if possible

Upvotes: 4

Views: 2530

Answers (2)

Syphdias
Syphdias

Reputation: 31

[Timer]
OnCalendar=Mon..Fri *-*-01
OnCalendar=Mon *-*-02..03

There are two OnCalendar conditions needed. This can be done in one timer.

  • Mon..Fri *-*-01 will activate if the first day of the month is a business day
  • Mon *-*-02..03 will activate if the second or third day of the month is a Monday. This happens if the first was a Sunday, or if the first was a Saturday and the second was a Sunday. There is no overlap.

To verify this you can use:

systemd-analyze calendar 'Mon..Fri *-*-01' 'Mon *-*-02..03' --iterations 10

I find it easier to verify if you filter and sort only the iterations by appending |grep Iteration |sort -k4, but this will cut off the first iteration.

You can do a very similar thing but in reverse with the last business day of the month:

[Timer]
OnCalendar=Mon..Fri *-*~01
OnCalendar=Fri *-*~02..03

With systemd v233 you can specify a day relative to the end of the month. Instead of using - before the day you use ~.

  • Mon..Fri *-*~01 triggers if the last day of the month is a business day
  • Fri *-*~02..03 triggers if the second to last or third to last day of the month was a Friday. This happens if the weekend is on the last day of the month.

Again, you can verify this using:

systemd-analyze calendar 'Mon..Fri *-*~01' 'Fri *-*~02..03' --iterations 10

Upvotes: 3

Polizi8
Polizi8

Reputation: 277

[Timer]
OnCalendar=Mon..Fri *-*-1..3 00:00:00

This executes the service

  1. only if it is a business day
  2. only the first 3 days of the month

The intersection of these directives leads to the first business day (Monday to Friday) of every month.

Examples:

  • Day 1 is Saturday, day 2 is Sunday, day 3 is Monday. Script runs the 3rd
  • Day 1 is Sunday, day 2 is Monday. Script runs the 2nd
  • Day 1 is Thursday. Script runs the 1st

Source: ArchWiki

Upvotes: 3

Related Questions