473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

mysql and cron

Hi.

I am a newbie to mysql, cron, and shell scripting, so please bear with
me. I have a script that is used for updating an image directory based
on contents in a database. The script does the following: runs
several queries against different tables in a database; returns several
lists of pictures being used in the database; removes any obsolete
images from specific directories. The script is bash shell script.

The problem is that I can run this script from the command line without
any problems. After testing this script several times, I decided to
put into crontab so that it can execute daily. From crontab, it will
not execute the mysql commands in the file saying that "no commands
were found".

Error Log:

scriptname.sh: line #: mysql: command not found

My sql command in my script is as follows:

mysql --user=$USERNAME --password=$PASSWORD --host=$DBHOST dbname
<<COMMANDS
SELECT PHOTO INTO OUTFILE "filename" FROM table_name WHERE PHOTO != ""
AND PHOTONAME LIKE "\%0";
COMMANDS

Also, please note that I tried this command without 'PHOTONAME LIKE
"\%0"' to see if the % was causing any problems, and even with that
removed, it would not execute the mysql statements in this script.

Any help would be appreciated. Thank you so ever much for any help.

Natalie

Jan 9 '06 #1
3 10282
>I am a newbie to mysql, cron, and shell scripting, so please bear with
me. I have a script that is used for updating an image directory based
on contents in a database. The script does the following: runs
several queries against different tables in a database; returns several
lists of pictures being used in the database; removes any obsolete
images from specific directories. The script is bash shell script.

The problem is that I can run this script from the command line without
any problems.
What is the value of $PATH when you run the script from the command line?
Where is the mysql command? /usr/local/bin/mysql ?
After testing this script several times, I decided to
put into crontab so that it can execute daily. From crontab, it will
not execute the mysql commands in the file saying that "no commands
were found".
What is the value of $PATH when you run the script under cron?
/bin:/usr/bin ?

Error Log:

scriptname.sh: line #: mysql: command not found


Try setting PATH (and export it) or using an absolute pathname for
the command.

Gordon L. Burditt
Jan 9 '06 #2

<th*********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi.

I am a newbie to mysql, cron, and shell scripting, so please bear with
me. I have a script that is used for updating an image directory based
on contents in a database. The script does the following: runs
several queries against different tables in a database; returns several
lists of pictures being used in the database; removes any obsolete
images from specific directories. The script is bash shell script.

The problem is that I can run this script from the command line without
any problems. After testing this script several times, I decided to
put into crontab so that it can execute daily. From crontab, it will
not execute the mysql commands in the file saying that "no commands
were found".

Error Log:

scriptname.sh: line #: mysql: command not found

My sql command in my script is as follows:

mysql --user=$USERNAME --password=$PASSWORD --host=$DBHOST dbname
<<COMMANDS
SELECT PHOTO INTO OUTFILE "filename" FROM table_name WHERE PHOTO != ""
AND PHOTONAME LIKE "\%0";
COMMANDS

Also, please note that I tried this command without 'PHOTONAME LIKE
"\%0"' to see if the % was causing any problems, and even with that
removed, it would not execute the mysql statements in this script.


If you can do it on the command line, then it will work in crontab.
My first guess would be that you are running crontab for a different user
than the one where you did your command line test - the one that worked so
well.

try
crontab -l
at the same command line that worked so well. Does it print out the cron
that you set up?
My guess is it won't because you entered it in someone else cron.
Did I guess correctly ?

Every user (including "root") has his/her own cron.
I don't know why, but most people seem to think they have to run cron as
"root". They test the command line as [natalie] (for example!) then enter
the same command line in the [root] cron and wonder why it doesn't work.
The reason is that [root] runs in a different environment than [natalie] and
the search $PATH probably doesn't contain the path to the mysql utility.

One solution is to give the full path to the mysql utility instead of just
"mysql".
Better would be to run it in [natalie]'s own personal cron rather than
root - or someone elses.
crontab -e
at the same command prompt you use to test will put you into edit mode for
that particular users cron.

Better yet - just edit your cron in a plain text file. Then:
crontab <MyCron.txt
to push it into your own [natalie] cron.
crontab -l
will show it back at you.

You might also consider that -
[mysql] is set up as it's own user on the system and has, of course, it's
own cron available.
It might be appropriate to log in as [mysql] and use *that* crontab to do
mysql maintenance.

Never do anything as [root] unless you absolutely have to !
Thomas Bartkus
Jan 9 '06 #3
try
crontab -l
at the same command line that worked so well. Does it print out the
cron
that you set up?
My guess is it won't because you entered it in someone else cron.
Did I guess correctly ?

================
Thomas,

The crontab and script are run/owned by the same user (not root). And
the account does have a plain text cron job that's pushed onto the
crontab. I never use root unless necessary and only su into it since
that's the only way you can get to root (disabled both remote login and
console login for root)... :)

But that is very good advise. And in genereal anyone not doing this
should. Thank you.
================
What is the value of $PATH when you run the script from the command
line?
Where is the mysql command? /usr/local/bin/mysql ?
================
Gordon,

It was the PATH. I fixed it and now everything works. Thank you.
================

Jan 10 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Neil | last post by:
Hi, I hope this question isn't too far off topic....I'm almost at my wits end trying to figure this out. I have a Mysql database and I wish to automate the backup of the database because I...
4
by: steve | last post by:
I have an on-going problem that my ".MYI" mysql files get corrupted. I have written php trapping routine to report on the problem to admin. My next step is to initiate "repair table" right after...
3
by: Mark Adams | last post by:
I am a relative newbie to MySQL. I had a Postfix+Courier+MySQL mail server running for several months. It took me a week or so to get it up and running in September. Now, I did a clean upgrade to...
3
by: dburdick | last post by:
I was referred to this forum as this may be a php specific problem. Some kind of configuration problem maybe. I have some php files that connect to a mysql database and are run as cron jobs. ...
3
by: Sandman | last post by:
I have a PHP cli script, that backups my databases, using mysqldump via system(). When logged in with SSH, and launching the script with "./backup.php", all works just as it should, but when I...
0
by: Pratchaya | last post by:
In my.cnf i add these lines ####### log-bin log-slow-queries = /var/log/mysqld-slow.log long_query_time=1 #######
7
RedSon
by: RedSon | last post by:
Hi guys, I don't really know anything about cron jobs but I need something that will check to make sure my mysql server is up and running every few minutes or so. Or maybe one that can detect if...
2
by: mezise | last post by:
Posted by Pratchaya: ------------------------------------------------------ MySQL Slow Log ERROR In my.cnf i add these lines ####### log-bin log-slow-queries = /var/log/mysqld-slow.log
4
by: Steve | last post by:
Can someone help me with this code - I'm trying to retrieve updated product information by pulling 3 fields and inserting values into my MYSQL db. In my code below I'm getting the page but I can't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.