linux 程序调用system执行命令

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

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

}

 

posted @ 2016-09-13 20:38  你且浅笑相安  阅读(4194)  评论(0编辑  收藏  举报