1

I have a server running Linux operating system. I am trying to schedule a cron job, in crontab file, to run a task every two weeks (Fortnight) on Tuesday at 9 am. I tried to run the following command:

0 9 * * 2   root    test $((10#$(date +\%V)\%2)) -eq 0 && ( java -jar /email/emailRemind.jar )

This script does not work, it shows this message

/bin/sh: 1: arithmetic expression: expecting EOF: 10#24%2"

Any thoughts?

underscore_d
  • 6,309
  • 3
  • 38
  • 64
  • 1
    I'm guessing indicating the base within `$((...))` with `$(( base#number ))` is a bashism, and the default shell for cron is not Bash; try setting the `SHELL` environment variable to bash, like `SHELL=/bin/bash` or whatever the correct path is. – Benjamin W. Jun 09 '20 at 15:04
  • In the beginning of crontab file, I already have this SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin. Is this what do you mean? –  Ibrahim EL-Sanosi Jun 09 '20 at 16:27
  • Exactly – it has to be `bash`, not `sh`. The shell your machine has at `/bin/sh` doesn't support arithmetic expansion with different bases. Alternatively, if you use GNU `date`, you can suppress the leading zero with `%-V` and avoid having to set the base 10 explicitly. – Benjamin W. Jun 09 '20 at 16:30
  • Do you mean I should change SHELL=/bin/sh to SHELL=/bin/bash? –  Ibrahim EL-Sanosi Jun 09 '20 at 16:49
  • Yes, that's what I mean, or change the formatting parameter for `date`. – Benjamin W. Jun 09 '20 at 17:36
  • I have changed to SHELL=/bin/bash, I get Error: Unable to access jarfile /emailReminders/emailRemind.jar. How to change the formatting parameter for date? –  Ibrahim EL-Sanosi Jun 09 '20 at 17:38
  • `/email/emailRemind.jar` is an absolute path, is the file really there? Do you have a top level directory called `/email`? – Benjamin W. Jun 09 '20 at 17:42
  • Yes, the absolute path /email/emailRemind.jar is there. Yes, I have top-level directory called email. –  Ibrahim EL-Sanosi Jun 09 '20 at 17:48
  • To clarify, my absolute path is /emailReminders/emailRemind.jar not /email/emailRemind.jar.. I mentioned directory email as example here. –  Ibrahim EL-Sanosi Jun 09 '20 at 17:56
  • Your code above has `/email/emailRemind.jar`, your error message is `/emailReminders/emailRemind.jar`. Whatever you use in the cron has to be the path to an existing file. – Benjamin W. Jun 09 '20 at 19:18
  • Yes, I am aware with this, so I use this path /emailReminders/emailRemind.jar and it exists my server. But the error "Unable to access jarfile /emailReminders/emailRemind.jar" is causes by something else, I am still investigating and looking for help as well. –  Ibrahim EL-Sanosi Jun 09 '20 at 19:57
  • Yes, when I use SHELL=/bin/bash, it works! Thank you –  Ibrahim EL-Sanosi Jun 11 '20 at 23:56

0 Answers0