5 Ways to Keep Remote SSH Sessions and Processes Running After Disconnection

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.95/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Gunjit Khera

Currently a Computer Science student and a geek when it comes to Operating System and its concepts. Have 1+ years of experience in Linux and currently doing a research on its internals along with developing applications for Linux on python and C.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

11 Responses

  1. konrad says:

    with setsid, how do I bring that session to the foreground?

  2. Raghavendra Rao says:

    Sorry to say but this article is simply ignoring the in-home features the come with open-ssh itself. Though you are sharing couple of other commands (which require packages to be installed) and revealing ways to maintain ssh sessions, but the irony is that an ideal sys admin or architect should have an expertise over the day-to-day commands that one uses to interact with the linux servers.

    Coming to the point, ssh itself works on the feedback received from the TCPALive packets and based on it, it gives out the errors of either “connection timed out, refused, disconnecting and several other known ones.” So a good troubleshooter should directly catch hold of the parent and fix it so that all the child services work as intended.

    Add the following entry in /etc/ssh/ssh_config of the client:

    ServerAliveInterval 30

    After that, restart the service on the client.

    This will send the TCPalive packet every 30 seconds to the server and will thus keep the connection ongoing and active unless we manually disconnect it or the network itself breaks down.

  3. Salvato says:

    Hi, I found this article very useful.
    P.S. In nohup reattaching section it looks like you posted the same PuTTy screenshot of the attach section..

  4. Edric says:

    Excellent article!

    You can check out a compiled tmux cheat sheet at https://edricteo.com/tmux-cheat-sheet/

  5. David Okwii says:

    I use byobu which is based on tmux for this sort of thing. Byobu is a robust alternative to screen or tmux

  6. L P says:

    The easiest way, if you know ahead of time, is to fork the ssh after authentication, with the ‘-f’ flag:

    $ ssh -f [email protected] ‘sleep 1; echo hi’
    Password:
    $ hi

    redirecting stdout & stderr is recommended, either on the local side or remote.

    • Gunjit Khera says:

      Hi! thanks for your suggestion, this is also a good way for running ssh connection if you want to execute a particular command on remote host.
      Stay Connected.. :-)

  7. Hil Palmer says:

    Another way I run background jobs and don’t want it to die when I exit is to send it to at.

    Example: echo “/tmp/script_name.sh” | at now

  8. Paul says:

    Good article, thanks=)

    P.S. In tmux reattaching section comes tmux detach again. I think you mean attach =)

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *