SSH 分叉和子项

发布于 2024-12-05 23:32:33 字数 1154 浏览 1 评论 0原文

我有一个程序,可以通过 ssh 连接到服务器并获取数据。这是代码...我分叉它,子级执行查询,父级等待子级预定的时间(在函数超时中),然后杀死子级。我这样做是因为有时,我不太清楚为什么,但 ssh 连接会停止并且不会退出。那就是进程列表中有一个“ssh -oConnectTimeout=60 blah blah”很长一段时间,并且超时功能似乎不起作用。我在这里做错了什么?上次出现此问题时,进程列表中有一个 ssh 已经 5 天了,但它仍然没有超时,并且程序已停止,因为它正在等待子进程。有那些额外的 wait() 函数,因为之前我遇到了很多失效的进程,也称为僵尸进程。所以我采取了简单的方法..

c = fork();
if(c==0) {
    close(fd[READ]);

    if (dup2(fd[WRITE],STDOUT_FILENO) != -1)
        execlp("ssh", "ssh -oConnectTimeout=60", serverDetails.c_str(), NULL);
    _exit(1);

}else{
    if(timeout(c) == 1){
        kill(c,SIGTERM);
        waitpid(c, &exitStatus, WNOHANG);
        wait(&exitStatus);
        return 0;
    }
    wait(&exitStatus);
}

这就是超时功能。

int timeout(int childPID)
{
int times = 0, max_times = 10, status, rc;
while (times < max_times){
    sleep(5);
    rc = waitpid(childPID, &status, WNOHANG);
    if(rc < 0){
        perror("waitpid");
        exit(1);
    }
    if(WIFEXITED(status) || WIFSIGNALED(status)){
        /* child exits */
        break;
    }
    times++;
}

if (times >= max_times){
    return 1;
}
else return 0;
}

I have a program where i ssh into a server and gets data. Here is the code... I fork it and the child executes the query and the parent waits for the child for a predetermined amount of time (in function timeout) and then kills the child. I did that part because sometimes, i am not exactly sure why, but the ssh connection stops and doesnot exit. That is there is a "ssh -oConnectTimeout=60 blah blah" in the processes list for a long and the timeout function doesnt seem to work. What am i doing wrong here? The last time this problem occured, there was an ssh in process list for 5 days and still it didnot timeout and the program had stopped because it was waiting for the child. There are those extra wait() functions because previously i was getting a lot of defunct processes a.k.a zombies. So i took the easy way out..

c = fork();
if(c==0) {
    close(fd[READ]);

    if (dup2(fd[WRITE],STDOUT_FILENO) != -1)
        execlp("ssh", "ssh -oConnectTimeout=60", serverDetails.c_str(), NULL);
    _exit(1);

}else{
    if(timeout(c) == 1){
        kill(c,SIGTERM);
        waitpid(c, &exitStatus, WNOHANG);
        wait(&exitStatus);
        return 0;
    }
    wait(&exitStatus);
}

This is the timeout function.

int timeout(int childPID)
{
int times = 0, max_times = 10, status, rc;
while (times < max_times){
    sleep(5);
    rc = waitpid(childPID, &status, WNOHANG);
    if(rc < 0){
        perror("waitpid");
        exit(1);
    }
    if(WIFEXITED(status) || WIFSIGNALED(status)){
        /* child exits */
        break;
    }
    times++;
}

if (times >= max_times){
    return 1;
}
else return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

另类 2024-12-12 23:32:33

SIGTERM 只是要求礼貌地终止该进程。如果它被卡住了,那么它不会对此做出响应,你需要使用 SIGKILL 来杀死它。可能是在尝试了 SIGTERM 并等待了一会儿之后。

另一种可能性是它正在等待到父进程的输出管道未满 - 也许有足够的输出来填充缓冲区,并且子进程正在等待该输出而不是网络。

SIGTERM just asks for a polite termination of the process. If it's got stuck, then it won't respond to that, and you'll need to use SIGKILL to kill it. Probably after trying SIGTERM and waiting a little while.

The other possibility is that it's waiting for the output pipe to the parent process to not be full - maybe there's enough output to fill the buffer, and the child is waiting on that rather than the network.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文