Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run a Crontab job on odd/even weeks 1

Status
Not open for further replies.

MikeDave888

Programmer
Feb 4, 2020
7
US
Hello everyone,

I hope you can help me with the issue I am having. I tried to run a Crontab job on odd/even weeks but couldn't get it to work. Below has the detail. I'd greatly appreciate any feedback that you can provide. Thank you...

# The job ran and the script got executed.
15 17 * * 1 test 0 -eq 0 && /path/script_name.sh
# But when I tried to run the script on even weeks, the script didn't get executed.
15 17 * * 1 test $(echo $(($(date +%V) % 2))) -eq 0 && /path/script_name.sh

# The job ran and the script got executed.
15 17 * * 1 test 1 -eq 1 && /path/script_name.sh
# But when I tried to run the script on odd weeks, the script didn't get executed.
15 17 * * 1 test $(echo $(($(date +%V) % 2))) -eq 1 && /path/script_name.sh

I also tested the following and they work but when using them within the Crontab job they seem not to be working.

oracle@testdb:/home/oracle> if test $(echo $(($(date +%V) % 2))) -eq 0; then echo "0"; else echo "1"; fi
0
oracle@testdb:/home/oracle> if test $(echo $(($(date +%V -d '2020/01/27') % 2))) -eq 0; then echo "0"; else echo "1"; fi
1
oracle@testdb:/home/oracle> if test $(echo $(($(date +%V -d '2020/02/03') % 2))) -eq 0; then echo "0"; else echo "1"; fi
0

Thanks and Regards,
Mike
 
Hi

In Vixie cron compatible implementations there would be one thing to mention :
man 5 crontab said:
Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.
So try escaping the percent signs in the commands.

If does not help, please specify which cron implementation you use and which shell is your cron using.


Feherke.
feherke.github.io
 
Yes, escaping the % with a backslash worked. :)

Thanks feherke, very much appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top