Issue
I have a Cron Job running quarterly on every first Sunday of the Month:
0 0 1-7 3,6,9,12 * [ "$(date '+\%a')" == "Sun" ] && /script.sh
Now I want to install it automatically using echo like:
(crontab -l ; echo "0 0 1-7 3,6,9,12 * [ "$(date '+\%a')" == "Sun" ] && /script.sh") | crontab -
while escaping \"Sun"\
works fine I can't escape "$(date '+\%a')"
as this is a mix of single and double quotes with \
within double quoted echo command.
Solution
You don't need to use crazy escaping. Use [[ ... ]]
provided by bash
instead of old (and error prone) [ ... ]
:
So use:
0 0 1-7 3,6,9,12 * [[ $(date +\%a) == Sun ]] && /script.sh
And to install it automatically use:
(crontab -l ; echo '0 0 1-7 3,6,9,12 * [[ $(date +\%a) == Sun ]] && /script.sh') | crontab -
PS: Make sure to set SHELL=/bin/bash
in the crontab file before this line.
Answered By - anubhava Answer Checked By - Robin (WPSolving Admin)