Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 如何触发ssh的结束?_Shell_Ssh - Fatal编程技术网

Shell 如何触发ssh的结束?

Shell 如何触发ssh的结束?,shell,ssh,Shell,Ssh,我使用代理服务器,每次都更改网络设置。 所以我想要像这样的shell脚本 使用_proxy(){ #变更网络 ssh代理-N #恢复更改 } 我试过像这样的trap命令 $ trap "echo 'detected!'" 2 $ ssh proxy -N 但是当我通过Ctrl+C完成ssh时,不会打印“detected!”。 有什么问题吗?当一个进程被中断时(例如从ctrl-c),该进程会收到SIGINT。如果您想知道子进程何时更改,请改为trapSIGCHLD: $ trap "echo

我使用代理服务器,每次都更改网络设置。
所以我想要像这样的shell脚本

使用_proxy(){
#变更网络
ssh代理-N
#恢复更改
} 
我试过像这样的
trap
命令

$ trap "echo 'detected!'" 2
$ ssh proxy -N
但是当我通过Ctrl+C完成ssh时,不会打印“detected!”。

有什么问题吗?

当一个进程被中断时(例如从ctrl-c),该进程会收到
SIGINT
。如果您想知道子进程何时更改,请改为trap
SIGCHLD

$ trap "echo int" SIGINT
$ trap "echo child" SIGCHLD
$ /bin/cat
ctrl+c
child

谢谢你的回答。我可以在bash中完成,但在zsh中不行。所以我通常使用zsh和defined-TRAPCHLD,但它不起作用。你有什么想法吗?@Kurarr我不使用zsh抱歉。在bash的案例中,thsutton的回答解决了这个问题。我用的是zsh,定义TRAPCHLD对我来说不起作用。我能解决它吗?