2
我在C中完成一個守護進程,它讀取標準輸出php內容並將內容保存到文件時遇到問題。這個問題只發生在PHP腳本文件上,而另一個則不會。
如果我手動調用腳本,您通常在stdout上編寫內容。C execl執行PHP腳本不在STDOUT中顯示結果
給叉後,我運行下面的代碼片段:
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,'\n');
if(collectorLog != NULL) trim(collectorLog);
tmLogPrintf(Log, TMLOG_LEVEL_ERROR , "On startScriptCollector collectorName : %s , collectorRaw : %s , collectorLog : %s...\n",
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...\n",
collectorName , str_raw);
exit(-1);
}
if(fd_error < 0){
tmLogPrintf(Log, TMLOG_LEVEL_ERROR , "Script %s not could save error log ...\n",
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);
}
}
感謝編輯! –