linux 程序调用system执行命令

时间:2021-06-17 16:52:55

正确使用system方法,判断返回值

int exeCmd(const char *cmd) {
pid_t status; status = system(cmd); if (- == status)
{
WriteLog("system error!");
}
else
{
WriteLog("exit status value = [0x%x]\n", status); if (WIFEXITED(status))
{
if ( == WEXITSTATUS(status))
{
WriteLog("run CMD script successfully.\n");
return ;
}
else
{
WriteLog("run CMD=[%s] script fail, script exit code: %d\n", cmd,WEXITSTATUS(status));
}
}
else
{
WriteLog("exit status = [%d]\n", WEXITSTATUS(status));
}
} return -; }