http://www.politiker-stopp.de/gfx/politiker-stopp-print.png

Benjamin Schieder

[SOLUTIONS] RUN CRONJOB EVERY OTHER SATURDAY

2010 February 15 | 4 comments

We just had this problem and no "obvious" solution:
How to run a cronjob every other Saturday?

The first step is easy, run a cronjob every Saturday:

0 0 * * 6 /usr/local/bin/job.bash


Then we thought about "guessing" when the first and third Saturday would be:
0 0 8-14,15-22 * 6 /usr/local/bin/job.bash


This works "mostly", but will fail about twice a year. So in the end we run the script every Saturday and add this snippet:
if [ -e /etc/job/runme ] ; then
rm -f /etc/job/runme
else
touch /etc/job/runme
exit -1
fi
...


This will run your cronjob once every other Saturday.


EOF

Category: blog

Tags: Solutions cronjob


4 Comments

From: foo
2010-02-15 23:17:56 +0100

0 0 * * 6 expr `date +%j` % 3 >/dev/null && /usr/local/bin/job.bash
(untested)

From: foo
2010-02-15 23:19:43 +0100

"% 2", of course.
Your comment was not sent
Possible reasons: You left a column blank, the comment is too short or you tried to spam me.
Note that referer spoofing also makes it impossible to post a comment.
Your comment will be held for moderator approval.

From: blindcoder
2010-02-16 12:00:45 +0100

@foo:
Unfortunately, that will not work over a year change. For example take the change 2010/2011:
blindcoder@fortuna:~$ date -d 20101225
Sat Dec 25 00:00:00 CET 2010
blindcoder@fortuna:~$ date -d 20110101
Sat Jan 1 00:00:00 CET 2011
blindcoder@fortuna:~$ date -d 20101225 +%j
359
blindcoder@fortuna:~$ date -d 20110101 +%j
001
blindcoder@fortuna:~$ expr `date -d 20101225 +%j` % 2
1
blindcoder@fortuna:~$ expr `date -d 20110101 +%j` % 2
1
This would cause the script to run at two consecutive Saturdays. On other years, it might cause the script to NOT run at two consecutive Saturdays.

From: foo
2010-02-16 21:46:03 +0100

Hmm, right..
But this should:
`date +%s` / 86400 % 2
I don't like the /etc/job/runme file..

Post a comment

All comments are held for moderation; basic HTML formatting is accepted.

Name: (required)
E-mail: (required, not published)
Website: (optional)
Comment: