Executing remote command just before remote server terminates connection

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











up vote
0
down vote

favorite












So I have searched for this but I didn't find anything which addresses this. I frequently connect to a remote server through ssh on which I don't have sudo permissions.



Let's say the connection gets terminated/reset by the remote server by inactivity or other reasons. Now all I want is that before the remote server terminates my connection it should run a pwd. I know there are ways to prolong/prevent remote server from terminating your connection, but I would like it to run a custom command of my choice before termination







share|improve this question























    up vote
    0
    down vote

    favorite












    So I have searched for this but I didn't find anything which addresses this. I frequently connect to a remote server through ssh on which I don't have sudo permissions.



    Let's say the connection gets terminated/reset by the remote server by inactivity or other reasons. Now all I want is that before the remote server terminates my connection it should run a pwd. I know there are ways to prolong/prevent remote server from terminating your connection, but I would like it to run a custom command of my choice before termination







    share|improve this question





















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      So I have searched for this but I didn't find anything which addresses this. I frequently connect to a remote server through ssh on which I don't have sudo permissions.



      Let's say the connection gets terminated/reset by the remote server by inactivity or other reasons. Now all I want is that before the remote server terminates my connection it should run a pwd. I know there are ways to prolong/prevent remote server from terminating your connection, but I would like it to run a custom command of my choice before termination







      share|improve this question











      So I have searched for this but I didn't find anything which addresses this. I frequently connect to a remote server through ssh on which I don't have sudo permissions.



      Let's say the connection gets terminated/reset by the remote server by inactivity or other reasons. Now all I want is that before the remote server terminates my connection it should run a pwd. I know there are ways to prolong/prevent remote server from terminating your connection, but I would like it to run a custom command of my choice before termination









      share|improve this question










      share|improve this question




      share|improve this question









      asked May 18 at 22:20









      Shachit Iyer

      32




      32




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          In Bash, you could set up a trap for EXIT to have the shell run a command when it exits:



          trap 'do something here' EXIT


          That works for shells exiting due to end of input (^D), timeout through TMOUT, being shot with a SIGTERM or SIGHUP, and remote shells over SSH where the SSH client dies.



          However, when running over SSH (or another network connection), the shell can't react to the connection closing before it's really closed, so anything it prints won't get to the other end at that point. You could run a command that modifies things on the remote end, but something like pwd, which just prints stuff, won't do anything useful.



          If you do want the last working directory shown, you could have the shell print it on the prompt, with the w escape.






          share|improve this answer





















          • Yes I was also thinking on similar lines that the shell cant catch imminent termination of connection. Thanks!
            – Shachit Iyer
            May 21 at 21:23










          • @ShachitIyer, yeah, especially a "reset by the remote server by inactivity" you mentioned sounds like something that probably would start by breaking the network connection, and then leaving the shell to clean up after itself when it sees the connection has gone away.
            – ilkkachu
            May 21 at 21:31

















          up vote
          0
          down vote













          Similarly to ilkkachu's answer, you may create ~/.bash_logout with a set of commands to run when the bash login shell exits. And similarly to the issue in his answer, this would not be run if the SSH connection was terminated.






          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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            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%2f444719%2fexecuting-remote-command-just-before-remote-server-terminates-connection%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            In Bash, you could set up a trap for EXIT to have the shell run a command when it exits:



            trap 'do something here' EXIT


            That works for shells exiting due to end of input (^D), timeout through TMOUT, being shot with a SIGTERM or SIGHUP, and remote shells over SSH where the SSH client dies.



            However, when running over SSH (or another network connection), the shell can't react to the connection closing before it's really closed, so anything it prints won't get to the other end at that point. You could run a command that modifies things on the remote end, but something like pwd, which just prints stuff, won't do anything useful.



            If you do want the last working directory shown, you could have the shell print it on the prompt, with the w escape.






            share|improve this answer





















            • Yes I was also thinking on similar lines that the shell cant catch imminent termination of connection. Thanks!
              – Shachit Iyer
              May 21 at 21:23










            • @ShachitIyer, yeah, especially a "reset by the remote server by inactivity" you mentioned sounds like something that probably would start by breaking the network connection, and then leaving the shell to clean up after itself when it sees the connection has gone away.
              – ilkkachu
              May 21 at 21:31














            up vote
            0
            down vote



            accepted










            In Bash, you could set up a trap for EXIT to have the shell run a command when it exits:



            trap 'do something here' EXIT


            That works for shells exiting due to end of input (^D), timeout through TMOUT, being shot with a SIGTERM or SIGHUP, and remote shells over SSH where the SSH client dies.



            However, when running over SSH (or another network connection), the shell can't react to the connection closing before it's really closed, so anything it prints won't get to the other end at that point. You could run a command that modifies things on the remote end, but something like pwd, which just prints stuff, won't do anything useful.



            If you do want the last working directory shown, you could have the shell print it on the prompt, with the w escape.






            share|improve this answer





















            • Yes I was also thinking on similar lines that the shell cant catch imminent termination of connection. Thanks!
              – Shachit Iyer
              May 21 at 21:23










            • @ShachitIyer, yeah, especially a "reset by the remote server by inactivity" you mentioned sounds like something that probably would start by breaking the network connection, and then leaving the shell to clean up after itself when it sees the connection has gone away.
              – ilkkachu
              May 21 at 21:31












            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            In Bash, you could set up a trap for EXIT to have the shell run a command when it exits:



            trap 'do something here' EXIT


            That works for shells exiting due to end of input (^D), timeout through TMOUT, being shot with a SIGTERM or SIGHUP, and remote shells over SSH where the SSH client dies.



            However, when running over SSH (or another network connection), the shell can't react to the connection closing before it's really closed, so anything it prints won't get to the other end at that point. You could run a command that modifies things on the remote end, but something like pwd, which just prints stuff, won't do anything useful.



            If you do want the last working directory shown, you could have the shell print it on the prompt, with the w escape.






            share|improve this answer













            In Bash, you could set up a trap for EXIT to have the shell run a command when it exits:



            trap 'do something here' EXIT


            That works for shells exiting due to end of input (^D), timeout through TMOUT, being shot with a SIGTERM or SIGHUP, and remote shells over SSH where the SSH client dies.



            However, when running over SSH (or another network connection), the shell can't react to the connection closing before it's really closed, so anything it prints won't get to the other end at that point. You could run a command that modifies things on the remote end, but something like pwd, which just prints stuff, won't do anything useful.



            If you do want the last working directory shown, you could have the shell print it on the prompt, with the w escape.







            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered May 18 at 23:11









            ilkkachu

            48k669132




            48k669132











            • Yes I was also thinking on similar lines that the shell cant catch imminent termination of connection. Thanks!
              – Shachit Iyer
              May 21 at 21:23










            • @ShachitIyer, yeah, especially a "reset by the remote server by inactivity" you mentioned sounds like something that probably would start by breaking the network connection, and then leaving the shell to clean up after itself when it sees the connection has gone away.
              – ilkkachu
              May 21 at 21:31
















            • Yes I was also thinking on similar lines that the shell cant catch imminent termination of connection. Thanks!
              – Shachit Iyer
              May 21 at 21:23










            • @ShachitIyer, yeah, especially a "reset by the remote server by inactivity" you mentioned sounds like something that probably would start by breaking the network connection, and then leaving the shell to clean up after itself when it sees the connection has gone away.
              – ilkkachu
              May 21 at 21:31















            Yes I was also thinking on similar lines that the shell cant catch imminent termination of connection. Thanks!
            – Shachit Iyer
            May 21 at 21:23




            Yes I was also thinking on similar lines that the shell cant catch imminent termination of connection. Thanks!
            – Shachit Iyer
            May 21 at 21:23












            @ShachitIyer, yeah, especially a "reset by the remote server by inactivity" you mentioned sounds like something that probably would start by breaking the network connection, and then leaving the shell to clean up after itself when it sees the connection has gone away.
            – ilkkachu
            May 21 at 21:31




            @ShachitIyer, yeah, especially a "reset by the remote server by inactivity" you mentioned sounds like something that probably would start by breaking the network connection, and then leaving the shell to clean up after itself when it sees the connection has gone away.
            – ilkkachu
            May 21 at 21:31












            up vote
            0
            down vote













            Similarly to ilkkachu's answer, you may create ~/.bash_logout with a set of commands to run when the bash login shell exits. And similarly to the issue in his answer, this would not be run if the SSH connection was terminated.






            share|improve this answer

























              up vote
              0
              down vote













              Similarly to ilkkachu's answer, you may create ~/.bash_logout with a set of commands to run when the bash login shell exits. And similarly to the issue in his answer, this would not be run if the SSH connection was terminated.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                Similarly to ilkkachu's answer, you may create ~/.bash_logout with a set of commands to run when the bash login shell exits. And similarly to the issue in his answer, this would not be run if the SSH connection was terminated.






                share|improve this answer













                Similarly to ilkkachu's answer, you may create ~/.bash_logout with a set of commands to run when the bash login shell exits. And similarly to the issue in his answer, this would not be run if the SSH connection was terminated.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Jul 26 at 10:33









                Kusalananda

                102k13199314




                102k13199314






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f444719%2fexecuting-remote-command-just-before-remote-server-terminates-connection%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Peggy Mitchell

                    Palaiologos

                    The Forum (Inglewood, California)