Statement returning error only launching the sh script via crontab


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Statement returning error only launching the sh script via crontab
# 1  
Old 02-27-2019
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; 02-27-2019 at 08:22 AM..
# 2  
Old 02-27-2019
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:
# 3  
Old 02-28-2019
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; 02-28-2019 at 05:03 AM..
# 4  
Old 02-28-2019
So - any success applying Jim McNamara's proposals?
# 5  
Old 02-28-2019
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 02-28-2019
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 02-28-2019
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; 02-28-2019 at 10:10 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Launching mplayer from within Links2 using a shell script

I'm using the Links2 console web browser in graphical mode (the "-g" argument), and launching a shell script that invokes MPlayer from within it. MPlayer works fine. No problem there. The problem, is that I have no control over the MPlayer process. I would like to be able to exit MPlayer whenever... (16 Replies)
Discussion started by: ignatius
16 Replies

2. Shell Programming and Scripting

[Solved] FOR loop / IF statement returning error

The code at the bottom is a simplified example of what we have. If I use the following: && echo "echo failed" $? returns 1 When I use if ; then echo "echo failed" ; fi $? returns 0 Does anyone know what's wrong with this? Using AIX 6.1 and KSH for NUM in 1 2 3 do ... (5 Replies)
Discussion started by: jfxdavies
5 Replies

3. UNIX for Advanced & Expert Users

[Solved] Crontab not launching script

Hi all, i have the following script #!/bin/sh for i in `ps -leaf --cols 1024 | grep LogUser | grep -v grep | awk '{print $4}'`; do echo $i kill -15 $i; done; but it seems that the crontab its sciping this script,i configured corntab as following */30 * * * root... (2 Replies)
Discussion started by: charli1
2 Replies

4. Shell Programming and Scripting

error in shell script while returning values-- urgent issue plz help.

Hi, I have initailized a varaible EBID as typeset Long EBID=0 i am calculating value of EBID using certian formula as below: (( CURR_EBID= ($BANDINDEX << 27) | ($CURR_FREQ << 16) | ($CURR_CELLID << 4) | $CURR_SECTOR_VALUE )) return $CURR_EBID The output is as below: + (( CURR_EBID=... (6 Replies)
Discussion started by: kasanur
6 Replies

5. Shell Programming and Scripting

Script returning an error message on exiting

Hi, I am writing a script in which I am using an IF-Else statement. Code sample: # Check for the product. If (test "$3" = "Pet") Then Product_Code="PI" elif (test "$3" = "Breakdown") Then Product_Code="RI" elif (test "$3" = "Travel") Then Product_Code="TI" ... (2 Replies)
Discussion started by: bghosh
2 Replies

6. Shell Programming and Scripting

shell script returning error code 2 from AIX to Mainframe

Hi, I have a shell script which is residing on AIX which is triggered by Mainframe through Connect Direct. The shell script creates several files and sends those files to mainframe using Connect Direct. The shell script is working fine, still it is returning exit code 2 to mainframe. What... (0 Replies)
Discussion started by: Yogesh Aggarwal
0 Replies

7. AIX

Error launching c++ executable

Hi everybody, I have compiled my code based on the ALPS libraries () and CMake build system with IBM compiler xlC 11.1 on AIX 6.1 at the latest patch level running on an IBM SP Power 6 machine for high-performance computing. When I run the executable I receive this error message: -bash-3.2$... (2 Replies)
Discussion started by: JessicaAlfonsi
2 Replies

8. Shell Programming and Scripting

Syntax error in script with if statement

I'm working on a function in a shell script I'm writing that will eventually take in and print out a list of vendor names and aliases (for my work) Here's the function in question: addvendorandalias () { echo echo -n 'Would you like to create a new vendor list (y or n)? ' read answer... (3 Replies)
Discussion started by: Straitsfan
3 Replies

9. Shell Programming and Scripting

Launching a C program that needs input from a shell script

hi there, i need some help, i am trying to run a script to launch a C program and a Java program but before running both I want to get a user input and then invoke both programs with input received. In the programs the inputs are not command line arguments. This is the code, after the java... (4 Replies)
Discussion started by: momal
4 Replies

10. UNIX for Dummies Questions & Answers

launching script via REXEC

Hi folks! my client uses an winapplication which is launching shell-scripts remotely on a HP-Unix Machine via Rexec. The application-configuration is launching the script (which is in the home directory of connecting user) like: rexec host user pass shell.sh So far so good, everything... (3 Replies)
Discussion started by: JohnMurdoch
3 Replies
Login or Register to Ask a Question