douyun1860 2015-09-23 19:28
浏览 32
已采纳

C execl执行PHP脚本不要在STDOUT中显示结果

I'm having trouble with a daemon done in C , which reads the stdout php content and saves the contents to a file. The problem happens only for a PHP script file, while another does not.
If I invoke the script manually, you write the contents on stdout normally.

After giving fork , I am running the following code snippet:

void startScriptCollector(char *php , char *collectorName , char *collectorRaw , char *collectorLog){
    char str_script[80];
    char str_raw[80];
    char str_log[80];
    int result = 0;
    int fd_output , fd_error; //File Descriptor

    setreuid(geteuid(), geteuid());

    //stat_rtrim(collectorLog);
    //removeChar(collectorLog,'
');
    if(collectorLog != NULL) trim(collectorLog);

    tmLogPrintf(Log, TMLOG_LEVEL_ERROR , "On startScriptCollector collectorName : %s , collectorRaw : %s , collectorLog : %s...
",
            collectorName, collectorRaw , collectorLog);
            //exit(-1);

    sprintf(str_script  , "%s/%s" , SLAVIEW_CUSTOM_SCRIPTS , collectorName);
    sprintf(str_raw     , "%s/%s" , SLAVIEW_CUSTOM_TMPCOL   , collectorRaw);
    sprintf(str_log     , "%s/%s" , VARDIR                  , collectorLog);

    fd_output = open(str_raw, O_CREAT | O_WRONLY , 0777);
    fd_error  = open(str_log, O_CREAT | O_WRONLY , 0777);

    if(fd_output < 0){
            tmLogPrintf(Log, TMLOG_LEVEL_ERROR , "Script %s not could save data in %s...
",
            collectorName , str_raw);
            exit(-1);
    }

    if(fd_error < 0){
            tmLogPrintf(Log, TMLOG_LEVEL_ERROR , "Script %s not could save error log ...
",
            collectorName);
            //exit(-1);
    }

    dup2(fd_output , 1); //write on data  file
    dup2(fd_error  , 2); //write on error file

    result = execl(PHPEXEC , "php" , str_script  , NULL);


    if(result == -1){
            tmLogPrintf(Log, TMLOG_LEVEL_ERROR , "Error, on execute script  %s ...", collectorName);
            exit(-1);
    }
}
  • 写回答

1条回答 默认 最新

  • douwen3965 2015-09-27 13:23
    关注

    I discovered the reason you're giving error . It was the relative path to the PHP libs . Or I could change the path programmatically through the c or through PHP and everything would be fixed! Then a friend who works with me add fullpath tested on PHP and the script ran normally.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件