×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Run a Crontab job on odd/even weeks

Run a Crontab job on odd/even weeks

Run a Crontab job on odd/even weeks

(OP)
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

RE: Run a Crontab job on odd/even weeks

Hi

In Vixie cron compatible implementations there would be one thing to mention :

Quote (man 5 crontab)

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

RE: Run a Crontab job on odd/even weeks

(OP)
Yes, escaping the % with a backslash worked. smile

Thanks feherke, very much appreciated.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close