Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?
share|improve this question
add comment

2 Answers 2

up vote 8 down vote accepted

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).

share|improve this answer
    
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 '13 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 '13 at 11:14
    
+1 great explanation! –  slm Dec 20 '13 at 14:02
add comment

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.

Update

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.

share|improve this answer
1  
While the advice to use screen is sound, it doesn't directly answer the two questions listed by the OP. –  Barun Dec 20 '13 at 14:43
    
+1, updated my answer. –  BigHomie Dec 20 '13 at 15:18
add comment

Your Answer

 
discard

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.