1

I' m a newbee here. My problem is::

if [[ "$stringA" == *$stringB* ]] is running on CLI, but not on cronjob.

the failure code is:

/home/pi/stromcounter_jahresmonatsdatei.sh: 67: 
/home/pi/stromcounter_jahresmonatsdatei.sh: [[: not found

cron doesn't find [[ in line 67

I can't explan myself. Perhaps, somebody can answer in german.

Kumar Saurabh
  • 2,297
  • 5
  • 29
  • 43
Markus
  • 11
  • 1
  • Why are you using `[[ ]]` brackets, normally they are not portable. Also please provide your full code. (Warum nutzest du den doppel brackets, sie sind normalaweise nicht portable, bitte stell dein kompletes code in der Frage) – deimus Dec 01 '15 at 08:56
  • Given the error's reference to `stromcounter_jahresmonatsdatei.sh`, this looks like the offending code isn't actually in your crontab, but is in an extra script. That means that script needs to have an appropriate shebang (starting with `#!/bin/bash`), and to be started the correct way (`/path/to/yourscript`, or `bash /path/to/yourscript`, not `sh /path/to/yourscript`). – Charles Duffy Dec 03 '15 at 20:49
  • Thanks you all freaks The solution was in crontab a line with bash /path/to/script. I wish all a happy christmas and a happy new year. – Markus Dec 10 '15 at 06:46

1 Answers1

1

[[ is a bash command, not a sh command. You will need to tell cron to use bash as its shell. Put the following at the top of the crontab with the correct location of bash:

SHELL=/path/to/bash
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Thank you for the fast answers. In the next day i will try it with the bash entry on cron. Best regards Markus – Markus Dec 02 '15 at 11:22
  • Sorry, but SHELL=/bin/bash in cron doesn't run. – Markus Dec 03 '15 at 20:35
  • @Markus, the answer Ignacio proposed most certainly does work with vixie cron, inasmuch as it ensures that cron directly starts bash. (Needs to be in the header, not any particular job; and if the code invoked by cron directly then starts another shell, then the identity of that shell matters as well; right now, you're not showing your work enough for anyone to identify possible errors in that respect). – Charles Duffy Dec 03 '15 at 20:47
  • @Markus, ...and what you mean by "doesn't run" isn't clear either; it's not *expected* that the line be run in an external shell; rather, the expectation is that it will be exported in the environment in jobs started by cron, and that `cron` will honor it in selecting the shell to use to execute code embedded in the crontab file itself (which is different from changing the shell used to execute shell scripts called by that code). – Charles Duffy Dec 03 '15 at 20:53