How to keep a SSH session for dynamic forwarding alive and terminate it at will? [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0















I found someone keeps a SSH session alive by



ssh -f -D $port $SSH_HOST "
if [ -f ~/.tunnel ]; then
rm ~/.tunnel;
fi;
while [ ! -f ~/.tunnel ]; do
echo > /dev/null;
done
" &


and terminates a SSH session by



ssh $SSH_HOST "touch ~/.tunnel" 


  1. Is the way of using a file the same way as a pid file? Can the way be used for more or general cases?


  2. Do we really need to use the way of using a file to keep a SSH session alive and terminate it? Is there a better way to do that?


Thanks.










share|improve this question















closed as too broad by user1133275, X Tian, Mr Shunz, Jeff Schaller, Thomas Feb 9 at 9:25


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






















    0















    I found someone keeps a SSH session alive by



    ssh -f -D $port $SSH_HOST "
    if [ -f ~/.tunnel ]; then
    rm ~/.tunnel;
    fi;
    while [ ! -f ~/.tunnel ]; do
    echo > /dev/null;
    done
    " &


    and terminates a SSH session by



    ssh $SSH_HOST "touch ~/.tunnel" 


    1. Is the way of using a file the same way as a pid file? Can the way be used for more or general cases?


    2. Do we really need to use the way of using a file to keep a SSH session alive and terminate it? Is there a better way to do that?


    Thanks.










    share|improve this question















    closed as too broad by user1133275, X Tian, Mr Shunz, Jeff Schaller, Thomas Feb 9 at 9:25


    Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.




















      0












      0








      0








      I found someone keeps a SSH session alive by



      ssh -f -D $port $SSH_HOST "
      if [ -f ~/.tunnel ]; then
      rm ~/.tunnel;
      fi;
      while [ ! -f ~/.tunnel ]; do
      echo > /dev/null;
      done
      " &


      and terminates a SSH session by



      ssh $SSH_HOST "touch ~/.tunnel" 


      1. Is the way of using a file the same way as a pid file? Can the way be used for more or general cases?


      2. Do we really need to use the way of using a file to keep a SSH session alive and terminate it? Is there a better way to do that?


      Thanks.










      share|improve this question
















      I found someone keeps a SSH session alive by



      ssh -f -D $port $SSH_HOST "
      if [ -f ~/.tunnel ]; then
      rm ~/.tunnel;
      fi;
      while [ ! -f ~/.tunnel ]; do
      echo > /dev/null;
      done
      " &


      and terminates a SSH session by



      ssh $SSH_HOST "touch ~/.tunnel" 


      1. Is the way of using a file the same way as a pid file? Can the way be used for more or general cases?


      2. Do we really need to use the way of using a file to keep a SSH session alive and terminate it? Is there a better way to do that?


      Thanks.







      ssh port-forwarding






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 7 at 3:42









      user1133275

      3,518723




      3,518723










      asked Feb 6 at 14:10









      TimTim

      27.4k78264474




      27.4k78264474




      closed as too broad by user1133275, X Tian, Mr Shunz, Jeff Schaller, Thomas Feb 9 at 9:25


      Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









      closed as too broad by user1133275, X Tian, Mr Shunz, Jeff Schaller, Thomas Feb 9 at 9:25


      Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






















          2 Answers
          2






          active

          oldest

          votes


















          3














          Simple Improvements



          The technique allows you to kill a connection from the remote machine. I would suggest a couple of changes



          • Add a sleep in the while loop, there's no point throwing CPU time on waiting

          • Invert the process. Touch the file at the start and delete it to kill the session. This does give you an option to include a PID in the file if you want.


          Do you need this?



          If you only want to kill the session I would avoid the method in your question and use:



          SSH -N -D $port $SSH_HOST &


          This will set up a connection without a shell as a background task (-N disables the shell and & makes it a background task).



          You can get the (local) PID for this with $! and kill it with kill



          Eg:



          SSH -N -D $port $SSH_HOST &
          TUNNEL_PID=$!

          # Do some other stuff

          kill $TUNNEL_PID





          share|improve this answer
































            1














            Besides what @PhilipCouling has written, there exists an odd but effective little utility, Spinner, useful when an interactive terminal session is to be kept open. You run it on the remote host. It doesn't do much, but sends a dummy data packet back to the local client every N seconds (I use N = 120, for example). The dummy keeps the connection alive.



            Not sure that this is the sort of thing you wanted, but there it is.






            share|improve this answer





























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              3














              Simple Improvements



              The technique allows you to kill a connection from the remote machine. I would suggest a couple of changes



              • Add a sleep in the while loop, there's no point throwing CPU time on waiting

              • Invert the process. Touch the file at the start and delete it to kill the session. This does give you an option to include a PID in the file if you want.


              Do you need this?



              If you only want to kill the session I would avoid the method in your question and use:



              SSH -N -D $port $SSH_HOST &


              This will set up a connection without a shell as a background task (-N disables the shell and & makes it a background task).



              You can get the (local) PID for this with $! and kill it with kill



              Eg:



              SSH -N -D $port $SSH_HOST &
              TUNNEL_PID=$!

              # Do some other stuff

              kill $TUNNEL_PID





              share|improve this answer





























                3














                Simple Improvements



                The technique allows you to kill a connection from the remote machine. I would suggest a couple of changes



                • Add a sleep in the while loop, there's no point throwing CPU time on waiting

                • Invert the process. Touch the file at the start and delete it to kill the session. This does give you an option to include a PID in the file if you want.


                Do you need this?



                If you only want to kill the session I would avoid the method in your question and use:



                SSH -N -D $port $SSH_HOST &


                This will set up a connection without a shell as a background task (-N disables the shell and & makes it a background task).



                You can get the (local) PID for this with $! and kill it with kill



                Eg:



                SSH -N -D $port $SSH_HOST &
                TUNNEL_PID=$!

                # Do some other stuff

                kill $TUNNEL_PID





                share|improve this answer



























                  3












                  3








                  3







                  Simple Improvements



                  The technique allows you to kill a connection from the remote machine. I would suggest a couple of changes



                  • Add a sleep in the while loop, there's no point throwing CPU time on waiting

                  • Invert the process. Touch the file at the start and delete it to kill the session. This does give you an option to include a PID in the file if you want.


                  Do you need this?



                  If you only want to kill the session I would avoid the method in your question and use:



                  SSH -N -D $port $SSH_HOST &


                  This will set up a connection without a shell as a background task (-N disables the shell and & makes it a background task).



                  You can get the (local) PID for this with $! and kill it with kill



                  Eg:



                  SSH -N -D $port $SSH_HOST &
                  TUNNEL_PID=$!

                  # Do some other stuff

                  kill $TUNNEL_PID





                  share|improve this answer















                  Simple Improvements



                  The technique allows you to kill a connection from the remote machine. I would suggest a couple of changes



                  • Add a sleep in the while loop, there's no point throwing CPU time on waiting

                  • Invert the process. Touch the file at the start and delete it to kill the session. This does give you an option to include a PID in the file if you want.


                  Do you need this?



                  If you only want to kill the session I would avoid the method in your question and use:



                  SSH -N -D $port $SSH_HOST &


                  This will set up a connection without a shell as a background task (-N disables the shell and & makes it a background task).



                  You can get the (local) PID for this with $! and kill it with kill



                  Eg:



                  SSH -N -D $port $SSH_HOST &
                  TUNNEL_PID=$!

                  # Do some other stuff

                  kill $TUNNEL_PID






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Feb 6 at 14:47

























                  answered Feb 6 at 14:41









                  Philip CoulingPhilip Couling

                  1,255918




                  1,255918























                      1














                      Besides what @PhilipCouling has written, there exists an odd but effective little utility, Spinner, useful when an interactive terminal session is to be kept open. You run it on the remote host. It doesn't do much, but sends a dummy data packet back to the local client every N seconds (I use N = 120, for example). The dummy keeps the connection alive.



                      Not sure that this is the sort of thing you wanted, but there it is.






                      share|improve this answer



























                        1














                        Besides what @PhilipCouling has written, there exists an odd but effective little utility, Spinner, useful when an interactive terminal session is to be kept open. You run it on the remote host. It doesn't do much, but sends a dummy data packet back to the local client every N seconds (I use N = 120, for example). The dummy keeps the connection alive.



                        Not sure that this is the sort of thing you wanted, but there it is.






                        share|improve this answer

























                          1












                          1








                          1







                          Besides what @PhilipCouling has written, there exists an odd but effective little utility, Spinner, useful when an interactive terminal session is to be kept open. You run it on the remote host. It doesn't do much, but sends a dummy data packet back to the local client every N seconds (I use N = 120, for example). The dummy keeps the connection alive.



                          Not sure that this is the sort of thing you wanted, but there it is.






                          share|improve this answer













                          Besides what @PhilipCouling has written, there exists an odd but effective little utility, Spinner, useful when an interactive terminal session is to be kept open. You run it on the remote host. It doesn't do much, but sends a dummy data packet back to the local client every N seconds (I use N = 120, for example). The dummy keeps the connection alive.



                          Not sure that this is the sort of thing you wanted, but there it is.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Feb 6 at 15:01









                          thbthb

                          524314




                          524314












                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay