1
我已經編寫了C程序,它使用fork(2)和execl(3)爲端口轉發目的運行ssh。 ssh在後臺運行-f選項。如何終止後臺的ssh實例
當C程序退出時,我希望它發送SIGTERM到它生成的ssh實例。
我已經試過
// creating the ssh process
ssh_pid = fork();
if (ssh_pid == 0)
execl("/usr/bin/ssh", "/usr/bin/ssh", "-f", other options, NULL)
// ...
printf("Killing %d\n", ssh_pid); // <--- output the PID
kill(ssh_pid, 15);
sleep(1); // give it a chance to exit properly
if (kill(ssh_pid, 0) == 0)
kill(ssh_pid, 9); // the "shotgun" approach
然而,這並沒有(甚至與SIGKILL)工作。
如果我運行PS 程序退出
ps aux | grep ssh | grep -v sshd | grep -v grep
之前,我看到這樣的事情:
user 27825 0.2 0.0 0 0 pts/0 Z+ 18:23 0:00 [ssh] <defunct>
user 27834 0.0 0.0 41452 1176 ? Ss 18:23 0:00 /usr/bin/ssh -f [other options]
當程序打印它殺死PID,我看到:
Killing 27825
隨後重複ps給我:
user 27834 0.0 0.0 41452 1176 ? Ss 18:23 0:00 /usr/bin/ssh -f [other options]
看起來原來的ssh已經爲了成爲後臺進程而分叉了。
所以我改變了我的電話殺(2)試圖殺死由原始的SSH產生的所有進程:
kill(-ssh_pid, 15);
但是這似乎沒有任何效果。我懷疑這是因爲原來的ssh不再是背景音樂ssh的父母。
那麼,我該如何安全地殺死背景的ssh?它甚至有可能嗎?
我決定使用[libssh2](http://www.libssh2.org/)而不是執行大量的ssh(1)進程。 – szmoore 2013-02-19 08:11:58
我認爲更好的解決方案是使用SSH的「ControlMaster」功能。我回答了一個類似的問題[這裏](http://stackoverflow.com/a/26470428/1072112)。 – ghoti 2015-08-18 04:12:38