configure a biweekly cron job

Status
Not open for further replies.

woodshop2300

Dabbler
Joined
Apr 21, 2016
Messages
46
Hi all.

I'm looking for a way to configure a biweekly job (every other week).
I currently do this on a linux box using this cron line.

Code:
0 0 * * sat root expr $(( $( date +\%s ) / 86400 )) \% 2 >/dev/null || echo "" | mail -s "Some Subject" your_email@blah.org


When i try to replicate this on FreeNAS the "test" run of the job works ok.
But when cron tries over night i only got an error message.
So i tried dropping one of the parentheses making it.

Code:
0 0 * * sat root expr $( $( date +\%s ) / 86400 ) \% 2 >/dev/null || echo "" | mail -s "Some Subject" your_email@blah.org


Still got an error mail.
The subject was
Code:
Cron <games@Nas> PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/root/bin" expr $( $( date +\

the body was
Code:
Syntax error: end of file unexpected (expecting ")")
Syntax error: Error in command substitution
Syntax error: Error in command substitution


I'm not sure what the issue is.
It could be it needs translating from Linux to FreeBSD, or Bash to Sh, or both.
Could also be something to do with entering it with the web form?
Maybe its a protection i'm tripping somehow?
 

aviator

Cadet
Joined
Apr 12, 2016
Messages
2
Unless otherwise specified, your crontab runs with sh not bash. $(...) is a bash-ism instead of back ticks. You might also need the full path to mail.

Try:
Code:
0 0 * * sat root [ `expr \`date +\%s\` / 86400 \% 2` -eq 0 ] || echo "" | /usr/bin/mail -s "Some Subject" your_email@blah.org
 

woodshop2300

Dabbler
Joined
Apr 21, 2016
Messages
46
All right thanks, now that i know its an sh prompt for sure at least i can play with things on the ssh shell prompt instead of making weird cron jobs to try stuff.
Anyway, still don't work, when it ran last night still got an error.
Code:
Syntax error: EOF in backquote substitution
Syntax error: Error in command substitution

And when i just run it at the prompt i also get an error.
Code:
[root@Nas] ~# [ `expr \`date +\%s\` / 86400 \% 2` -eq 0 ] || echo "" | /usr/bin/mail -s "Some Subject" your_email@blah.org
Unmatched `.

playing around with it, and doing some googling it seems like none of the nested back tick examples are working right..
I'll have to play with it some more.
 

aviator

Cadet
Joined
Apr 12, 2016
Messages
2
Hmmm :(. I might have hallucinated when it worked for me in a jail.
Let's try again.

From the GUI: Tasks -> Cron Jobs -> Add Cron Job:
User: root
Command:
Code:
/bin/bash -c '[[ $(( ($(date +%s) / 86400) % 2 )) ]] && mail -s "Some Subject" your_email@blah.org'

Minute: Each selected minute: 0
Hour: Each selected hour: 0
Day of Month: every N day of month: 1
Month: check all
Day of week: Saturday
Redirect stdout and stderr: your choice
Enabled: yes

Since bash is installed on freenas, let's use it!

The important part is the single quotes around the bash command. They ensure that the csh (root's shell) will not interpret anything between them.

After adding the above task, you can see what actually gets placed in the crontab by entering the following in a command shell
Code:
cat /etc/crontab

You will see that the percent characters have been escaped for you.

To provide an explanation for the rational for the command to anyone interested...
It takes the date in seconds since the epoch, integer divides it to get days since the epoch, and then determines whether there have been an even or odd number of days since the epoch by computing the remainder of dividing by 2, i.e. the modulus. Since there are 7 days in a week [citation needed], the same day of the week alternates between even and odd days. As the cronjob is set to only run on one day a week, the command gets triggered each week, but the test only passes every other week (in this case when the day in question falls on an even number of days since the epoch. Therefore, the mail command is only executed every other week.
 

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
Stop using one-liners, do a real script, use variables, use /bin/sh and use POSIX syntax, everything will be fine and will not break with every change you make to it ;)
 

woodshop2300

Dabbler
Joined
Apr 21, 2016
Messages
46
Well, after putting the echo back in all is working.
No errors at night and it tripped this morning like it should have.
 
Status
Not open for further replies.
Top