简体   繁体   English

C ++获取在后台运行的进程失败

[英]C++ Get failure for process running in background

I'm starting a script from my app and using & at the end of the command to run the process in the background: 我正在从我的应用程序启动脚本,并在命令末尾使用&在后台运行该过程:

std::string pCommand = "python myscript.py &";
int ret = system(pCommand.c_str());

if (ret == 0)
{
    //No error, continue...
}

However even if the script fails, the return from system() is always 0. If I remove the & so it does not run in the background, then I do get the correct exit code on failure. 但是,即使脚本失败, system()的返回值始终为0。如果我删除了&使其不在后台运行,则在失败时我会获得正确的退出代码。

Is there a way to detect if the process failed, or is that simply not possible when executing a background process with system() ? 有没有办法检测进程是否失败,或者用system()执行后台进程根本不可能?

如果在后台运行它,请在脚本结束之前检查ret的值,而ret的默认值可能是0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM