0

I noticed a number of similar questions ask and I also tried the suggestions posted, but I just can't seem to get this working. Below is my code.

#!/bin/bash

while IFS="" read -d "" -r filename;
do
filearray[i++]="$filename"
done < <(find . -maxdepth 1 -type f -iname "*.txt" -print0)
printf '%s\n' "${filearray[0]}"

Simply, I just want to search for all txt type files in a specific directory and put them into an array, which is displayed at the end. When I run from the command line, no problem. It works well. As soon as I execute this via crontab, I get the following error:

syntax error near unexpected token `<'
`done < <(find . -maxdepth 1 -type f -iname "*.txt" -print0)'

Here's the cron entry itself:

* * * * * . /usr/online/scripts/test.sh 2>> /usr/online/scripts/log/test.log

Why is the script working on command line, but not crontab? I am declaring the shell, so surely crontab should use the shell? Is there another why I can do this which works?

Jakkie

  • How do you invoke it in the crontab? – Kusalananda Feb 14 '17 at 10:41
  • 1
    Do you set SHELL=/bin/bash in the crontab? Otherwise you're getting /bin/sh behavior – Jeff Schaller Feb 14 '17 at 10:41
  • I have tried that as well, not no luck. I will quickly do it again and check. – Jakkie Feb 14 '17 at 10:43
  • Sorry, are you saying I must add SHELL=/bin/bash at the top of my crontab file, or the script? – Jakkie Feb 14 '17 at 10:46
  • Just a few commands we run. They all work fine by the way. This particular script is the only one using the line causing the error. – Jakkie Feb 14 '17 at 11:02
2

command < <(other command) is a bashism. Since you are specifying a shebang line, you should not specify a shell in your crontab; that's just confusing. You should do one or the other - I very much prefer a shebang line because you're being explicit about the syntax that follows, rather than pretending to be a "generic" shell script.

Oh, and make sure you're making your script executable and putting only /path/to/your/script.sh in the crontab command field, not sh /path/to/your/script.sh, sh < /path/to/your/script.sh or even . /path/to/your/script.sh. The last three ignore the shebang line, and instead runs the script in the context of the cron shell, whatever that is set up to be.

See redirection instructions for more info on saving script output to a file.

  • OK, thanks. So what is wrong then? The command I run from crontab is: * * * * * . /usr/online/scripts/test.sh 2>> /usr/online/scripts/log/test.log – Jakkie Feb 14 '17 at 10:47
  • is that with, or without the "dot", as in . / – Jakkie Feb 14 '17 at 10:50
  • I amended the crontab command as you suggested, but now the log output file is empty. Was the script executed? – Jakkie Feb 14 '17 at 10:52
  • There is no way for me to tell from the available information, but I would very much expect so. Have you checked your cron logs? – l0b0 Feb 14 '17 at 10:54
  • Im sort of new to Linux. Where can I check the crontab logs? Am I correct in saying that if I added the 2>> part to export to a log, that only errors will be sent to the output, not the actual steps? If that is true, then the script should be working now, because the output is zero bytes. – Jakkie Feb 14 '17 at 10:56
0

Assuming the script is executable and is doing the right thing when invoked from the command line, write your crontab entry like this (to run every 15th minute):

*/15 * * * * /path/to/script.sh >/path/to/output 2>/path/to/errors

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.