homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

how to set up a mysql command as a cron job?
for example a find and replace

 2:41 am on Sep 17, 2010 (gmt 0)

I run this command a lot

update [table_name] set [field_name] = replace([field_name],'[string_to_find]','[string_to_replace]');

but would prefer it to run by itself, every day.

how would I set this as a cron job?



 7:39 am on Sep 17, 2010 (gmt 0)

I believe it will be something like this:

Put your command in a text file, such as runupdate.sql

Then your cron command will be something like this:

mysql -uYOURUSER -pYOURPASSWORD < /full/path/runupdate.sql

This page might be helpful, and shows how to do the same thing as the "<" but using command line switches:


The above assumes that the MySQL Command Line Client is installed on your server. I don't know if that is the usual case on shared hosting servers, but it might be.

If you get an error that the mysql program doesn't exist on the server, you could put your command in a .php script (along with the necessary connection code, etc.) and run that from cron. I'm less sure of the cron syntax for that, but it might be something like

php /full/path/runupdate.php

If I'm on the right track, I'm hoping others can fill in needed details or correct errors.


 1:25 pm on Sep 17, 2010 (gmt 0)

As SteveWh mentioned, simply run a Cron on a PHP file with the SQL update query within. That way, you can set your conditional statements using PHP, where using a command-line SQL dump, you cannot.


 1:04 am on Sep 18, 2010 (gmt 0)

thanks guys, giving it a shot now

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved