Executing remote command just before remote server terminates connection

Clash 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
ssh terminal bashrc
add a comment |Â
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
ssh terminal bashrc
add a comment |Â
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
ssh terminal bashrc
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
ssh terminal bashrc
asked May 18 at 22:20
Shachit Iyer
32
32
add a comment |Â
add a comment |Â
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.
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
add a comment |Â
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.
add a comment |Â
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered Jul 26 at 10:33
Kusalananda
102k13199314
102k13199314
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password