请问:在守护进程中启动其它应用程序后,如何捕获shell输出?

sanford81 2009-12-17 03:05:00
主程序中完成成为守护进程后的代码:
char m_array[128] = {0};
int retVal=-1;
int length=128;
retVal=GetStdOut(pCmd,m_array,&length);//pCmd为启动应用程序的命令行

int GetStdOut(char* cmd,char* outresult,int *outlen)
{
FILE *fd;
syslog(LOG_INFO,"%s","bbbbbbbbb"); //可以输出
int i=0;
char c;
fd = popen(cmd, "r");
if(fd != 0)
{
memset(outresult,0,*outlen);
while(fread(&c, sizeof(char), 1, fd) > 0)
{
syslog(LOG_INFO,"%s","aaaaaaaaaaaa"); //不能输出
syslog(LOG_INFO,"%c",c);
printf("%c", c);
*outresult++=c;
i++;
}
//clog_new.closefile();
pclose(fd);
}
*outlen=i;
if(i > *outlen)
return -1;
else
return 0;
}

我想把应用程序中shell中的输出,写入到日志文件中,结果什么也写不进去。程序到fd = popen(cmd, "r");好像就在一直等待应用程序执行结束。

另外,还有几个问题,一起请教一下:
1.守护进程中,怎么写文件,难道只能写syslog吗?
2.如何判断该应用程序是否已经启动过,我用下面的代码,不行:
sem_t *mutex;
mutex = sem_open ("mj_online_encoder_mutex", O_CREAT, 0644, 1);
if (mutex == SEM_FAILED){
Cxxlog clog_new("Daemon_log", M_DEFAULT, 0);
if(clog_new.print("已运行,不能同时运行两个")<=0)
clog_new.print("open log file Error 0!\n");
clog_new.closefile();
return -1;
}


请指教,谢谢!

...全文
261 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wang_chen_xi_cool 2010-02-08
  • 打赏
  • 举报
回复
谢谢共享!
我的小站:
http://www.wangchenxicool.yayapage.com/

~嵌入式/linux/arm/pic学习、交流 ... .... [嵌入式产品设计、制作] ~

----------来坐坐吧!^_^ (=^ ^=)
sourceid 2009-12-17
  • 打赏
  • 举报
回复

让我们来看看cron是如何来做.
原来它做了三件事,setgid,setuid,chdir.


switch (pid = fork()) {
case -1:
perror("fork");
goto fatal;
case 0:
/* child */
if (setgid(MY_GID(pw)) < 0) {
perror("setgid(getgid())");
exit(ERROR_EXIT);
}
if (setuid(MY_UID(pw)) < 0) {
perror("setuid(getuid())");
exit(ERROR_EXIT);
}
if (chdir(_PATH_TMP) < 0) {
perror(_PATH_TMP);
exit(ERROR_EXIT);
}
if (!glue_strings(q, sizeof q, editor, Filename, ' ')) {
fprintf(stderr, "%s: editor command line too long\n",
ProgramName);
exit(ERROR_EXIT);
}
execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", q, (char *)0);
perror(editor);
exit(ERROR_EXIT);
/*NOTREACHED*/
default:
/* parent */
break;
}


sanford81 2009-12-17
  • 打赏
  • 举报
回复
上面的代码中,如何让“被调用的应用程序”执行结束后再返回?并且在主进程中实时捕获“被调用的应用程序”的shell输出呢?
sanford81 2009-12-17
  • 打赏
  • 举报
回复
这段也不行,时间能写进去,但是调用的应用程序的输出获取不到。我刚接触linux,救急,谢谢!

int main(int argc,char *argv[])
{
init_daemon(argv[0],LOG_KERN);
time_t ticks;
while(1)
{
ticks=time(NULL);
syslog(LOG_INFO,"%s",asctime(localtime(&ticks)));
FILE *finput;
FILE *foutput;
int n;
finput=popen("/opt/ffmpeg/bin/ffmpeg -i /opt/ffmpeg/bin/cctv5.ts -threads 0 -vcodec libx264 -b 318k -bt 318k -r 25 -s 640x480 -vglobal 1 -vpre test -acodec libaacplus -ab 32k -ar 44100 -ac 2 -y /opt/ffmpeg/bin/cctv5-aa.ts.flv","r");
char pipe_buf[128] = {0} ;
while(fgets(pipe_buf, sizeof(pipe_buf), finput)) {
syslog(LOG_INFO,"%s",pipe_buf);
}
pclose(finput);
}
}
sanford81 2009-12-17
  • 打赏
  • 举报
回复
用这段代码,也写不了日志:


int main(int argc,char *argv[])
{
init_daemon(argv[0],LOG_KERN);
time_t ticks;
while(1)
{
ticks=time(NULL);

syslog(LOG_INFO,"%s",asctime(localtime(&ticks)));

FILE *finput,*foutput;
char buffer[PIPE_BUF];
int n;

finput=popen("/opt/ffmpeg/bin/ffmpeg -i /opt/ffmpeg/bin/cctv5.ts -threads 0 -vcodec libx264 -b 318k -bt 318k -r 25 -s 640x480 -vglobal 1 -vpre test -acodec libaacplus -ab 32k -ar 44100 -ac 2 -y /opt/ffmpeg/bin/cctv5-aa.ts.flv","r");
foutput=popen("cat","w");

read(fileno(finput),buffer,strlen("test!"));
write(fileno(foutput),buffer,strlen("test"));

syslog(LOG_INFO,"%s",buffer);

pclose(finput);
pclose(foutput);
}
}

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧