Statement returning error only launching the sh script via crontab


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 3 Weeks Ago
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:

Code:
#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:

Code:
/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!

Last edited by vbe; 3 Weeks Ago at 07:22 AM..
# 2  
Old 3 Weeks Ago
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:

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

Also, I guessed some things - please tell us your UNIX type and your shell
This User Gave Thanks to jim mcnamara For This Post:
RudiC (3 Weeks Ago)
# 3  
Old 3 Weeks Ago
Hi jim and many thanks for your help!

this is my environment:

my UNIX type:

Code:
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:
Code:
bash


Last edited by RudiC; 3 Weeks Ago at 04:03 AM..
# 4  
Old 3 Weeks Ago
So - any success applying Jim McNamara's proposals?
# 5  
Old 3 Weeks Ago
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
# 6  
Old 3 Weeks Ago
Quote:
Originally Posted by mfran2002
in anycase I don't find .profile but only .bash_profile:
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:

Code:
#! /path/to/shell

. /usr/local/lib/shell/f_env

<rest of the code>

I hope this helps.

bakunin
# 7  
Old 3 Weeks Ago
hi bakunin and thanks
yes it surely helps!

tomorrow morning I will try your suggestions and let you know

many thanks

Last edited by mfran2002; 3 Weeks Ago at 09:10 AM..
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Launching mplayer from within Links2 using a shell script ignatius Shell Programming and Scripting 16 11-09-2018 12:15 PM
Script gives error when scheduled in crontab scriptor UNIX for Beginners Questions & Answers 2 05-30-2017 03:01 AM
Not returning from a sub script? Charles Swart UNIX for Dummies Questions & Answers 1 01-23-2014 12:36 PM
[Solved] FOR loop / IF statement returning error jfxdavies Shell Programming and Scripting 5 09-02-2013 03:53 AM
[Solved] Crontab not launching script charli1 UNIX for Advanced & Expert Users 2 12-13-2012 09:15 AM
error in shell script while returning values-- urgent issue plz help. kasanur Shell Programming and Scripting 6 12-27-2011 05:14 AM
Script returning an error message on exiting bghosh Shell Programming and Scripting 2 11-15-2011 05:12 AM
shell script returning error code 2 from AIX to Mainframe Yogesh Aggarwal Shell Programming and Scripting 0 07-08-2011 06:53 AM
Error launching c++ executable JessicaAlfonsi AIX 2 03-14-2011 05:15 AM
Syntax error in script with if statement Straitsfan Shell Programming and Scripting 3 12-03-2010 10:39 PM
Launching a C program that needs input from a shell script momal Shell Programming and Scripting 4 11-06-2009 10:48 PM
Getting error when running script through crontab nypreH UNIX for Dummies Questions & Answers 4 10-28-2009 09:01 AM
launching script via REXEC JohnMurdoch UNIX for Dummies Questions & Answers 3 08-20-2008 05:42 AM
Usermod returning syntax error syndex Shell Programming and Scripting 6 07-02-2007 12:38 PM
returning to the parent shell after invoking a script within a script gurukottur Shell Programming and Scripting 5 09-26-2006 07:05 AM