LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 07-22-2012, 07:58 PM   #1
rch
Member
 
Registered: Feb 2003
Location: Santa Clara,CA
Distribution: Mandriva
Posts: 909

Rep: Reputation: 48
Script runs from shell -but not from crontab


I wrote a script that loads data from Mysql to Monetdb. It runs perfectly when I run the script from the shell but for some reason, it does not run from crontab. I have determined that the error comes from mysql binary (as if I redirect stderr when called from crontab, I get user([username]):user([username]): as error). I am thinking that I am missing defining some environmental variables.

Any help will be greatly appreciated.

Code:
#!/bin/bash
PATH=/bin:/usr/bin:/usr/sbin/:/usr/local/bin
SCRIPTWD=<SCRIPT DIR>
USER=<USER>
MYSQL_HOST=<HOST>
topt="-n +2"
password=<PASSWORD>
cat $SCRIPTWD/tables| (while read p;
do
mysql -h<HOST> -P3306 -u$USER -p$password -B -e "select * from $p;" <db_name> | sed "s/\"/\"\"/g;s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g;s/\"NULL\"//g;s/<9d>//g"|iconv -f latin1 -t utf-8//ignore  > "$SCRIPTWD/$p.csv"
tail $topt "$p"".csv">"$SCRIPTWD/$p""_new.csv"
rm "$SCRIPTWD/$p"".csv"
mclient -d<db_name> -s "delete from $p;"
mclient -d<db_name> -E utf8 -s "COPY INTO $p from '$SCRIPTWD/""$p""_new.csv' USING DELIMITERS ',','\n','\"' NULL AS ''"
rm "$SCRIPTWD/$p""_new.csv"
done)

Last edited by rch; 07-22-2012 at 08:00 PM.
 
Old 07-22-2012, 08:47 PM   #2
wpeckham
Member
 
Registered: Apr 2010
Location: USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix
Posts: 832

Rep: Reputation: 181Reputation: 181
Cron

Cron executes the posix version of a shell (/bin/sh) with the default environment. There is no logon environment, so USER is not defined, PATH may differ, and other factors. A careful study of the ENVIRONMENT section and start-up sections of the shell man page should give you a clue as to what is going on.

In short, define the needed environment variables in the script itself to ensure that the values and calls are as you need them.
 
Old 07-22-2012, 09:00 PM   #3
rch
Member
 
Registered: Feb 2003
Location: Santa Clara,CA
Distribution: Mandriva
Posts: 909

Original Poster
Rep: Reputation: 48
Thank wpckeham, I know that USER is not defined- that's why I define USER (even though I don't think it is required for mysql client to run). I don't think that is the problem though. Do you know which environment variable am I missing here?
 
Old 07-23-2012, 12:53 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,948

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
try to debug

Code:
set -x
exec >/tmp/debug.$$ 2>&1
 
1 members found this post helpful.
Old 07-23-2012, 01:37 PM   #5
wpeckham
Member
 
Registered: Apr 2010
Location: USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix
Posts: 832

Rep: Reputation: 181Reputation: 181
Troubleshooting

Second that (set -x is your friend)
Make sure to check the expanded mysql line in that log, and make certain that the expansions and substitutions are as you expect.
 
Old 07-24-2012, 05:07 PM   #6
rch
Member
 
Registered: Feb 2003
Location: Santa Clara,CA
Distribution: Mandriva
Posts: 909

Original Poster
Rep: Reputation: 48
Thanks very much NevemTeve and wpeckham, the issue was not mysql (my bad), it was the location of the directory where the script is being run. The directory also has a .monetdb file which supplies the username and password for the Monetdb database (AFAIK, mclient does not have a password parameter). That was what was breaking it- and your suggestion helped me find the problem.
 
  


Reply

Tags
bash, crontab, mysql5


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash says "no such file or directory" when crontab runs the shell script ymnoor21 Linux - Newbie 16 03-23-2012 12:18 PM
Runs Multiple shell script inside a main script using crontab srimal Linux - Newbie 4 10-22-2009 07:19 PM
Shell script not working through crontab paragkalra *BSD 1 04-15-2009 01:36 PM
Shell Script: want to insert values in database when update script runs ring Programming 2 10-25-2007 11:48 PM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 11:23 PM


All times are GMT -5. The time now is 09:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration