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?

  • Just to let me understand, why do you want to persist a connection to MySQL to do it? May 1, 2013 at 19:03
  • That's what I'm looking for, yes.
    – Ed Marty
    May 1, 2013 at 19:59

1 Answer 1


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.

  • Nice, it worked here! +1 May 1, 2013 at 19:23
  • Very nice, exactly what I was looking for.
    – Ed Marty
    May 1, 2013 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, 2013 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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