首页 > 解决方案 > 如何通过系统调用发送 sigterm 来处理正在运行的 python

问题描述

我试图杀死一个为了运行我安装的 python 脚本而分叉的进程。在代码块终端中运行以及从命令行 (bash) 运行时,可以通过 crtl-c 终止此脚本。

我试过 SIGINT 和 SIGQUIT。当我不运行对 python 的系统调用时,它可以正常工作。

int main()
{
    int yahooTickerGrabberServicePID = fork();
    if (yahooTickerGrabberServicePID == 0)
    {
        system("YahooTickerDownloader.py");
        return EXIT_SUCCESS;
    }
    // sleep for testing only.
    sleep(5);
// Check to see if child is still running - 0 return indicates this is still running because the command can be sent...
        if(kill(yahooTickerGrabberServicePID, 0) == 0)
        {
            printf("good - still running...");
        }
        printf("sending sigint...");
        kill(yahooTickerGrabberServicePID, SIGINT);
        waitpid(yahooTickerGrabberServicePID, NULL, 0);
        while(processStillRunning == 0)
        {
            // printf("still running... :( ");
            kill(yahooTickerGrabberServicePID, SIGINT);
            processStillRunning = kill(yahooTickerGrabberServicePID, 0);
            waitpid(yahooTickerGrabberServicePID, NULL, 0);
        }
        printf("stopped?!");
        return EXIT_SUCCESS;
    }
}

我知道我需要对上述退货进行更多检查,这是一个完整的 MWE。

以上不会杀死脚本 - 我如何确保 SIGINT 进入从系统调用运行的 python,就像我从键盘敲击它时一样?

标签: pythonc++systemraspbiansigterm

解决方案


推荐阅读