[SOLVED] Script runs from shell -but not from crontab
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
cat $SCRIPTWD/tables| (while read p;
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"
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 ''"
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.
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?
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.