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












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





























    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



























      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 9 hours ago







      Tim

















      asked 10 hours ago









      TimTim

      26.6k77256465




      26.6k77256465






















          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























              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "106"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f499055%2fhow-to-keep-a-ssh-session-for-dynamic-forwarding-alive-and-terminate-it-at-will%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              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 10 hours ago

























                  answered 10 hours ago









                  Philip CoulingPhilip Couling

                  626412




                  626412

























                      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 9 hours ago









                          thbthb

                          524314




                          524314






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f499055%2fhow-to-keep-a-ssh-session-for-dynamic-forwarding-alive-and-terminate-it-at-will%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Callistus I

                              Tabula Rosettana

                              How to label and detect the document text images