How to launch script on First Saturday and two day before

Adriano Marcuz

New Member
Joined
May 7, 2019
Messages
1
Reaction score
0
Credits
15
Good morning all,
read several posts that this can't be done, but maybe someone has accomplished anyway.

What I need is to launch a script on the First Saturday of every month and two days before. For example first Saturday of December 2023 will happen on the second, I'd like my script to be launched on November 30th, December 1 and on December 2nd.
I can code a crontab for each say Saturday like ' 0 0 0 ? * 7#1 * /my script' and another for '0 0 0 ? * 6#1 * /my script' ......but wondered if anyone found a more efficient way as well as if the first Saturday happens on the 1st, then the coding for two days before becomes problematic.

Thanks for your input
 


This doesn't do exactly what you want, but it might be a way to get started in another direction.
I would put the logic for this in the script.

========================================
0 0 1 * * /path/to/your-script.sh
========================================

#!/bin/bash

# Get the current day of the week (0=Sunday, 1=Monday, ..., 6=Saturday)
day_of_week=$(date +'%w')

# Check if it's a Saturday (day_of_week = 6)
if [ $day_of_week -ne 6 ]; then
# Not a Saturday, so sleep for one day and run the script again
sleep 1d
exec "$0" "$@" # Re-run the script with the same arguments
else
# It's a Saturday, so perform your desired task
# Add your script logic here
fi

=======================================

This example doesn't solve all your requirements, but maybe with 2 or 3 crontabs,
and 2 or 3 scripts, you could get there.
 

Staff online

Members online


Top