How to keep a SSH session for dynamic forwarding alive and terminate it at will? [closed]
Clash Royale CLAN TAG#URR8PPP
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
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.
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
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.
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 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.
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
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
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 |
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
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
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
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
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
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
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
edited Feb 6 at 14:47
answered Feb 6 at 14:41
Philip CoulingPhilip Couling
1,255918
1,255918
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 Feb 6 at 15:01
thbthb
524314
524314
add a comment |
add a comment |