Zabba
Zabba

Reputation: 65497

How to run a SQL statement periodically?

I have a "raw" SQL statement that I need 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

Upvotes: 0

Views: 412

Answers (2)

kigster
kigster

Reputation: 620

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.

Upvotes: 1

guillaumepotier
guillaumepotier

Reputation: 7448

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

Upvotes: 1

Related Questions