How to keep a SSH session for dynamic forwarding alive and terminate it at will?
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"
Is the way of using a file the same way as a pid file? Can the way be used for more or general cases?
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
add a comment |
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"
Is the way of using a file the same way as a pid file? Can the way be used for more or general cases?
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
add a comment |
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"
Is the way of using a file the same way as a pid file? Can the way be used for more or general cases?
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
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"
Is the way of using a file the same way as a pid file? Can the way be used for more or general cases?
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
ssh port-forwarding
edited 9 hours ago
Tim
asked 10 hours ago
TimTim
26.6k77256465
26.6k77256465
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Simple Improvements
The technique allows you to kill a connection from the remote machine. I would suggest a couple of changes
- Add a
sleepin 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
add a comment |
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.
add a comment |
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
});
}
});
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
Required, but never shown
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
Simple Improvements
The technique allows you to kill a connection from the remote machine. I would suggest a couple of changes
- Add a
sleepin 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
add a comment |
Simple Improvements
The technique allows you to kill a connection from the remote machine. I would suggest a couple of changes
- Add a
sleepin 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
add a comment |
Simple Improvements
The technique allows you to kill a connection from the remote machine. I would suggest a couple of changes
- Add a
sleepin 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
Simple Improvements
The technique allows you to kill a connection from the remote machine. I would suggest a couple of changes
- Add a
sleepin 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
edited 10 hours ago
answered 10 hours ago
Philip CoulingPhilip Couling
626412
626412
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered 9 hours ago
thbthb
524314
524314
add a comment |
add a comment |
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.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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