I want a better way to do this, mostly for command line use:

while sleep 60; do 
  mysql -e 'show processlist'; 

This is better than:

while mysql -e 'show processlist'; do
  sleep 60;

... because if I type a ctrl-c, it stops. However, the first version waits for a minute before first output.

I know about watch. Often good, but not for all circumstances. I'm looking for a simple output to stdout, not curses. It'd be nice if watch had a --stdout option.

Simplicity is key here. I don't care about handling errors in the mysql statement. It can report its own errors to stdout.

  • 1
    And in second version it doesn't stop after Ctrl-C? – jimmij Dec 22 '14 at 11:48
  • 1
    Please edit your question and explain what you're trying to do. Your two commands are not equivalent (they will react differently if the mysql command return an error for example). – terdon Dec 22 '14 at 12:13
  • 1
    @jimmij it only stops if the ctrl-c causes the command in the while statement to fail. if that's the statement that runs only briefly, then the ctrl-c will probably land in the body of the loop, which doesn't stop the loop. – mc0e Dec 22 '14 at 16:13
  • @terdon they are similar if nothing fails. Different on failure, which is most likely when ctrl-c is pressed, which is kinda the point. sure there's no error checking for the mysql statement, but brevity is the key as this is for command line use. – mc0e Dec 22 '14 at 16:16
  • 1
    Yes, but I am still not sure what you want to do. It looks like you are trying to run the mysql command once a minute for ever. You also seem to have some requirements about how to stop it. Please edit and state what you need clearly. That will help both those who want to answer and anyone else who might search for similar functionality. – terdon Dec 22 '14 at 18:13

Ctrl+C does kill the whole loop under normal circumstances. The signal is sent to the whole foreground process group.

You may be looking for

while mysql -e 'show processlist' && sleep 60; do :; done

which exits as soon as either mysql or sleep fails.


This one might work for you:

trap "pkill -TERM -P $$" SIGINT

while [ 1 ]; do
   mysql -e 'show processlist'
   sleep 60
  • 1
    while true; or while :; are valid alternatives – glenn jackman Dec 22 '14 at 12:48
  • Doesn't help. ctrl-c doesn't kill the loop. mysql is quite capable of issuing its own error messages. – mc0e Dec 22 '14 at 16:17

Well, for your example...

n=; while sleep "$((n&(n=60)))"
do   mysql...

...will do it. On the first go it will sleep 0 and all of the rest will sleep 60. Still, I think probably there is a better way to reach your end goal than this.

If you did use something like this you'd probably want to do it in a function:

every_minute() ( n=
    while sleep "$((n&(n=60)))"
    do "$@"; done)

...or in a single line...

every_minute() (n=; while sleep "$((n&(n=60)))"; do "$@"; done)

If you put something like that in your shell's rc file you could just do...

every_minute mysql "$whatever" "$args" "$you" "$use"

...or you could just type out the single-line version at any prompt and continue to use it until you close the shell.

  • Kinda funky. I'm hoping there's something easier to remember correctly and to type though. – mc0e Dec 22 '14 at 16:20
  • @mc0e You plan to type this more often than once? You just need to put this in a function, man. – mikeserv Dec 22 '14 at 16:23
  • functions and aliases are cool, but I work across dozens of heterogeneous servers, many of which are not mine, and often I'm landing in an unfamiliar server. I want techniques I can use fast without installing stuff. I do have various things wrapped up in function statements I can paste in when needed, but if I don't need that then it's better. – mc0e Dec 22 '14 at 16:25
  • @mc0e - you really don't have to install a function. Just type that every_minute() definition at the prompt once and it's good for the life of the shell - or until you unset -f it. – mikeserv Dec 22 '14 at 16:31

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.