我如何杀死一个后台运行/独立SSH会话? [英] How do I kill a backgrounded/detached ssh session?
问题描述
我使用的程序协同与SSH隧道一起
I am using the program synergy together with an ssh tunnel
它的工作原理,我只需要打开一个控制台中键入以下两条命令:
It works, i just have to open an console an type these two commands:
ssh -f -N -L localhost:12345:otherHost:12345 otherUser@OtherHost
synergyc localhost
由于IM懒惰的我做了这是一个鼠标点击一个图标运行的Bash脚本:
because im lazy i made an Bash-Script which is run with one mouseclick on an icon:
#!/bin/bash
ssh -f -N -L localhost:12345:otherHost:12345 otherUser@OtherHost
synergyc localhost
在Bash脚本上述工作为好,但现在我也想杀死协同作用,通过一个鼠标点击的SSH隧道,所以我必须协同和ssh的PID保存到文件后,杀死他们:
the Bash-Script above works as well, but now i also want to kill synergy and the ssh tunnel via one mouseclick, so i have to save the PIDs of synergy and ssh into file to kill them later:
#!/bin/bash
mkdir -p /tmp/synergyPIDs || exit 1
rm -f /tmp/synergyPIDs/ssh || exit 1
rm -f /tmp/synergyPIDs/synergy || exit 1
[ ! -e /tmp/synergyPIDs/ssh ] || exit 1
[ ! -e /tmp/synergyPIDs/synergy ] || exit 1
ssh -f -N -L localhost:12345:otherHost:12345 otherUser@OtherHost
echo $! > /tmp/synergyPIDs/ssh
synergyc localhost
echo $! > /tmp/synergyPIDs/synergy
但这个脚本的文件是空的。
But the files of this script are empty.
我如何获得ssh和协同作用的PID?结果
(我会尽量避免的ps aux | grep的... | awk中... |。SED ...
组合,必须有一个更简单的方法)
How do I get the PIDs of ssh and synergy?
(I try to avoid ps aux | grep ... | awk ... | sed ...
combinations, there has to be an easier way.)
推荐答案
以及我不想添加和放大器;在命令的结尾仿佛控制台wintow被关闭的连接会死......所以我结束了一个PS-的grep,awk中,sed的-COMBO
well i dont want to add an & at the end of the commands as the connection will die if the console wintow is closed ... so i ended up with an ps-grep-awk-sed-combo
ssh -f -N -L localhost:12345:otherHost:12345 otherUser@otherHost
echo `ps aux | grep -F 'ssh -f -N -L localhost' | grep -v -F 'grep' | awk '{ print $2 }'` > /tmp/synergyPIDs/ssh
synergyc localhost
echo `ps aux | grep -F 'synergyc localhost' | grep -v -F 'grep' | awk '{ print $2 }'` > /tmp/synergyPIDs/synergy
(你可以grep的融入awk的,但我现在懒得)
(you could integrate grep into awk, but im too lazy now)
这篇关于我如何杀死一个后台运行/独立SSH会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!