[英]Catch exit code of external program in c++
我正在使用execvp
命令运行外部程序,现在我想捕获外部程序的退出代码,并在可能的情况下获取其PID
。
反正有可能吗?(我知道我可以在ubuntu中读取$?
并使用ps faxu
但这是肮脏的方式)
程序成功运行后, exec*
函数不会返回,因此您无法通过execvp
获取返回代码。 但是,如果你用fork
/ wait
,你可以得到从状态代码退出代码wait*
功能:
int status;
if (wait(&status) != -1) { // similar for waitpid, wait4, etc.
if (WIFEXITED(status)) {
exit_code = WEXITSTATUS(status);
} else {
// handle other conditions, e.g. signals.
}
} else {
// wait failed.
}
您可以在wait(2)的手册页中查看的示例。
也尝试int a_number = std::system("/path/to/app")
有时这可以用来返回xmessage查询的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.