Reputation: 10373
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
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 dayMon *-*-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 dayFri *-*~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
Reputation: 277
[Timer]
OnCalendar=Mon..Fri *-*-1..3 00:00:00
This executes the service
The intersection of these directives leads to the first business day (Monday to Friday) of every month.
Examples:
Source: ArchWiki
Upvotes: 3