Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to run a query every few seconds and show the output in a console window. From bash, using watch:

watch "mysql -A -pXXXXX -h host -D dbname -u username --vertical -e \"SELECT rowID FROM IDs LIMIT 1\""

With the output:

*************************** 1. row ***************************
rowID: 68434810

I'd like to not disconnect and reconnect every 2 seconds though. Is there a better way?

share|improve this question
Just to let me understand, why do you want to persist a connection to MySQL to do it? – altmannmarcelo May 1 '13 at 19:03
That's what I'm looking for, yes. – Ed Marty May 1 '13 at 19:59
up vote 5 down vote accepted

There's a cheap/low-tech way of doing this:

while [ 1 ] ; do echo "select rowid from ids limit 1;" ; sleep 2; done \
 | mysql -u ...

Ctrl C to interrupt.

share|improve this answer
Nice, it worked here! +1 – altmannmarcelo May 1 '13 at 19:23
Very nice, exactly what I was looking for. – Ed Marty May 1 '13 at 20:03
(BTW that select is suspicious. Unless IDs always has a single row (in which case the limit clause is redundant) or you need an order by for the result to be meaningful.) – Mat May 1 '13 at 20:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.