1

I'm having a strange situation where cron job is executing a script and doesn't read into the function. Yeah I know, solar flares.

On a serious note, the idea here that I have an .sh script which launches a scan of my environment. Below you can see the extract of it. The script successfully creates the output file and I can find this line echo "Is this line parsed or not?" >> $output in the output. However, the next echo which is in the function echo "I'm in the function!" >> $output is not logged. The script stop on the function declaration and that's it.

I'm baffled. Any ideas? Thanks!

.....
    touch $output
    # with this we are creating an empty file

    echo "Is this line parsed or not?" >> $output

    function do_the_scan() {
    # Load text file lines into a bash array.
    echo "I'm in the function!" >> $output
.....
  • Is your problem only when you call the script from cron or also when you launch it manually ? – Félicien Feb 22 '18 at 14:05
  • only from the cron. The script goes perfectly when launched manually. – will Feb 22 '18 at 14:30
  • 2
    At a guess, your script relies on one or more bashisms and cron is running it with dash. But it's impossible to say for sure given the lack of relevant information in your question. – steeldriver Feb 22 '18 at 14:39
3

Your function declaration syntax is wrong. You should use:

do_the_scan() {
    # Load text file lines into a bash array.
    echo "I'm in the function!" >> $output
}

…unless the script is run in Bash since function is a bashism.

This is a demonstration of the error in an interactive shell:

> sh
$ function bar() {
sh: 1: Syntax error: "(" unexpected
$

Anyway, you should capture the error output of your script using a standard error output redirection (e.g. 2> err.log). Put it directly on the command’s line in the crontab, so it can capture the errors produced by the interpreter of the script.

See also

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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