Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

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)

WebmasterWorld Senior Member 10+ Year Member

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)

5+ Year Member

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)

WebmasterWorld Senior Member 10+ Year Member

thanks guys, giving it a shot now