开发者

How to run a SQL statement periodically?

I have a "raw" SQL statement that I need开发者_Go百科 to execute in order to update status of objects (something like UPDATE users SET status=1 WHERE <some conditions>.

I need this to always run after every 60 seconds in the background.

How do I do this?

Ps. The environment is Ubuntu 10.10 and Rails 3.0.3


You'll have to use a cron (crontab) to periodically call a Rail script doing this update.


Loading rails environment to run one sql statement is a huge waste of resources.

Crontab for mysql:

0 * * * * mysql your_db_name -e "users SET status=1 WHERE <some conditions>;"

Crontab for PostgreSQL:

0 * * * * psql  -c "users SET status=1 WHERE <some conditions>" your_db_name

You may have to set PATH variable at the top of your crontab file to make sure that mysql/psql commands resolve.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜