popen创建一个额外的sh进程 [英] popen creates an extra sh process

查看:107
本文介绍了popen创建一个额外的sh进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用popen执行(分叉)命令,我看到的是,这里还有一个额外的sh -c "my_command process".

I am trying to execute (fork off) a command with popen and what I see is, there is an extra sh -c "my_command process" is also there.

我想最大程度地减少进程数,所以有可能摆脱它吗?

I want to minimize number of processes so is it possible to get rid of it?

ps输出:

root@home% ps awux | grep my_command
root 638  0.0  0.1  040  1424  ??  I    10:12PM   0:00.00 sh -c my_command /home/war
root 639  0.0  0.0  608   932  ??  S    10:12PM   0:00.01 my_command /home/war

阅读联机帮助页后,我知道这就是popen()的工作方式.

After reading manpage, I know this is how popen() works.

上述问题的答案由@R提供.

Answer to problem above was provided by @R..

我的要求是这样的,我需要将命令的输出转储到文件中并逐行读取该文件并处理输出.这就是为什么我使用popen的原因,因为它在文件中返回输出.我可以通过任何exec调用来实现吗?

My requirement is as such, I need to dump output of the command into a file and read that file line by line and process the output. This is why I am using popen because, it returns output in a file. Can I achieve that via any exec call?

推荐答案

您应该听那些劝告您不要使用popen的好朋友-这很糟糕.但是,对于遇到的问题,有一个简单的解决方法-将exec添加到传递给popen的命令行的开头.也就是说,代替:

You should listen to the good folks who are advising you not to use popen - it's bad. But there is a simple fix for the issue you've encountered - add exec to the beginning of the command line you pass to popen. That is, instead of:

popen("my_command /home/war", ...

使用:

popen("exec my_command /home/war", ...

这篇关于popen创建一个额外的sh进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆