[英]Catch exit code of external program in c++
I am running an external program with the command execvp
, now I want to catch the exit code of the external program and if possible get the PID
of it. 我正在使用
execvp
命令运行外部程序,现在我想捕获外部程序的退出代码,并在可能的情况下获取其PID
。
Is there anyway possible?(I know I can read $?
in ubuntu and use ps faxu
but these are dirty ways for that) 反正有可能吗?(我知道我可以在ubuntu中读取
$?
并使用ps faxu
但这是肮脏的方式)
The exec*
functions does not return when the program has successfully run, so you can't get the return code via execvp
. 程序成功运行后,
exec*
函数不会返回,因此您无法通过execvp
获取返回代码。 However, if you use fork
/ wait
, you could get the exit code from the status code in the wait*
functions: 但是,如果你用
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.
}
You could check the example of in the man page of wait(2) . 您可以在wait(2)的手册页中查看的示例。
Try also int a_number = std::system("/path/to/app")
也尝试
int a_number = std::system("/path/to/app")
This sometimes can be used to return the value of an xmessage query. 有时这可以用来返回xmessage查询的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.