Statement returning error only launching the sh script via crontab

hi all,
I created a sh script to import some tables from mysql to hive.
No problem launching it manually, but if I schedule via crontab it returns me an error in the following part:

#create an array containing all the tables for $dbname
query="SELECT table_name FROM information_schema.tables'

while read line
do
    if [ $line != "table_name" ]
        then
            tablesArray+=("$line")
    fi
done < <(mysql -h${conndb} -u${user} -p${psw} ${dbname} -e "${query}")

the error is:

/myscript.sh: line 72: syntax error near unexpected token `<'
/myscript.sh: line 72: `done <<(mysql -h${conndb} -u${user} -p${psw} ${dbname} -e "${query}")'

do you know what should I write?

thanks!

crontab uses /bin/sh, which may not work for you as a shell. This seems to be your problem (guess on my part). ALWAYS put a "shebang" as the first line of your script, and for cron scripts you probably need to source your personal .profile or .bashrc or whatever.

Example for first 2 lines:

#!/bin/bash
.  /path/to/mylogin/.profile

Also, I guessed some things - please tell us your UNIX type and your shell

1 Like

Hi jim and many thanks for your help!

this is my environment:

my UNIX type:

lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch
 Distributor ID: CentOS
Description:    CentOS Linux release 7.6.1810 (Core)
Release:        7.6.1810
Codename:       Core

my shell:

bash

So - any success applying Jim McNamara's proposals?

not yet tried, waiting for this evening

in anycase I don't find .profile but only .bash_profile:

. /home/myuser/.bash_profile

is it the same?
how can I be sure what is my profile path and file?

thanks

This is probably the relevant file.

.profile should be in your HOME directory and is a script which is executed every time you log on. It is executed by the login process itself.

.bashrc (or the similar .bash_profile ) is also located in your HOME directory and also a script. It is executed every time a new instance of the shell starts up. The first shell is started by the login process as the last part of logging you on to the system, therefore the two configuration files are often confused. But if you, say, start a new shell in your session typing bash , then .bashrc is execute another time in this new shell, whereas .profile is not.

Also notice that there are system-wide counterparts for both of these files (in CentOS /etc/profile and /etc/bashrc IIRC) that are also executed but prior to your own files. You can override them this way but if you don't you should get a "halfways decent" default configuration at least.

All these files are bypassed by by cron because it is started by the init process, not by login . I have a file f_env for this purpose which i always use to set a standard environment for scripts. This way i never have to worry about putting scripts in the crontab because they are always (not only in cron) using their own environment and not whatever i use for me personally. So my scripts always start with something like:

#! /path/to/shell

. /usr/local/lib/shell/f_env

<rest of the code>

I hope this helps.

bakunin

hi bakunin and thanks
yes it surely helps!

tomorrow morning I will try your suggestions and let you know

many thanks

sorry for the delay
tried this change:

35 10 * * * . $HOME/.bash_profile; /home/myscripts/myscript.sh

the same error

:frowning:

I told you to include the line

. $HOME/.bash_profile

into your script (myscript.sh) , not call it separately. This way two shells are called: first one shell which executes bash_profile, then this closes, then another shell which executes myscript.sh. The second one is of course not affected by any program that ran before.

And please, when you post code, data or terminal output, use CODE-tags as required by the forum rules.

I hope this helps.

bakunin

thanks and sorry for my oversight

I will try and let you know!

it works!

many thanks!

:slight_smile: