I've connected to a server over SSH and logged in to mysql, and asked it to run a query (the results of which are dumped into a CSV) that could take well over an hour. Although I have instructed my client to send null packets every 60 seconds to keep the session active, I am still paranoid the mysql query / process could terminate, so my questions are:

  1. Will a cancelled ssh session (due to inactivity) kill the mysql query process?
  2. How can I ensure this doesn't happen - are the null packet sends every 60 seconds enough?

2 Answers 2


Most shells send SIGHUP to the foreground process group on exit (and, in some, background processes as well, in bash this is controlled with the shell option huponexit), which might cause it to die, depending on how your mysql client handles that.

You can run your command prepended with nohup to have it reparented by init if your shell exits, regardless of whether your shell sends SIGHUP or not (being reparented by init is not related to nohup specifically -- it's just that it has outlived its parent).

Possibly a more palatable solution would be to use tmux, screen, dtach or similar to run the client abstracted away from your shell and controlling terminal. This way, if your shell disconnects, you just reconnect to the session that you were running the query in.

In general the keepalive packets won't matter, the connection won't just terminate for no reason. A more pressing concern would be a loss of connection between the client and the server for other reasons (network failure, etc).

  • Thankyou! Just for clarification, I'm running the actual query within mysql - so as long as I prepend nohup when logging in to mysql, this will persist the queries running inside?
    – njp
    Dec 20, 2013 at 11:12
  • 1
    @njp If you're wanting to run interactively, nohup won't be of much use to you, since the mysql REPL won't be attached to the terminal. Try using one of the multiplexers (or dtach) I mentioned above instead, they'll probably suit your needs better.
    – Chris Down
    Dec 20, 2013 at 11:14

I've started running screen sessions when I ssh into a box, partially to prevent that and as an added bonus, I get multiple tabs, can't beat it. If I'm disconnected for some reason, I can reattach to my screen session.


To answer #2, I would run mysql from within a bash shell through a screen session. Sounds complicated, but it's just:

$: screen
$: mysql

You can edit your ~/.screenrc file to add captions at the bottom of the screen session so you can keep track of your tabs, rename your tabs, etc. If you're ever disconnected, when you reconnect simply run

$: screen -d

and that will show any detached sessions. To reattach, simply run something like

$: screen -r 551.pts-0.git

Or whatever the id of the screen session was. You're right back where you left off. You must run screen -d as the same user that started the screen session though, just fyi, or root of course. I'm not really sure because after I ssh in I always sudo su - so I don't have to sudo every command.

  • 1
    While the advice to use screen is sound, it doesn't directly answer the two questions listed by the OP.
    – Barun
    Dec 20, 2013 at 14:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .