[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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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.