I'm trying to run an external shell command and read its output using the Boost libraries for C++ but it seems that either the command is not running or I just can't access the output. I'm using their documentation as example and wrote this:
#include <boost/process.hpp>
namespace bp = boost::process;
bool is_process_running(std::string p_name){
string cmd = "ps aux 2>&1";
bp::ipstream out;
std::string line;
bp::child c(cmd, bp::std_out > out);
// the while is supposed to read each line of the output
// but the execution doesn't enter here
while(c.running() && std::getline(out, line) && !line.empty())
{
if(line.find(p_name) != std::string::npos)
{
return true;
}
}
c.wait();
return false;
}
The goal is to verify each line output of ps aux
and search if a process is running. So, what could be the problem here? Or, can you provide a simple snippet for doing this?