juzzlin
juzzlin

Reputation: 47935

Boost 1.71.0: How to get process output?

I had code like this that worked fine on Ubuntu 18.04 and with Boost 1.65.0:

// See https://www.boost.org/doc/libs/1_65_0/doc/html/boost_process/tutorial.html
std::pair<int, std::string> runCommandAndGetOutput(const std::string & cmd, const std::vector<std::string> & args)
{
  namespace bp = boost::process;
  boost::asio::io_service ios;
  std::future<std::string> data;
  bp::child c(cmd, bp::args(args), bp::std_in.close(), bp::std_out > data, bp::std_err > bp::null, ios);
  ios.run();
  if (c.exit_code()) {
    std::cerr << "Command '" << cmd << "' failed with return value: " << c.exit_code();
  }
  return { c.exit_code(), data.get() };
}

However, after upgrading to Ubuntu 20.04 and Boost 1.71.0 this doesn't compile anymore because it seems that boost::asio::io_service is deprecated and doesn't exist anymore.

After googling I realized that I must use boost::asio::io_context instead.

Ok:

std::pair<int, std::string> runCommandAndGetOutput(const std::string & cmd, const std::vector<std::string> & args)
{
  namespace bp = boost::process;
  boost::asio::io_context ioc;
  std::future<std::string> data;
  bp::child c(cmd, bp::args(args), bp::std_in.close(), bp::std_out > data, bp::std_err > bp::null, ioc);
  ioc.run();
  if (c.exit_code()) {
    std::cerr << "Command '" << cmd << "' failed with return value: " << c.exit_code();
  }
  return { c.exit_code(), data.get() };
}

This compiles, but just doesn't work. Trying to run e.g. /usr/bin/free returns exit code 383 that doesn't make any sense.

What makes this difficult is that the documentation of Boost 1.71.0 is still using io_service:

https://www.boost.org/doc/libs/1_71_0/doc/html/boost_process/tutorial.html

Does anyone know how this should be done right?

Upvotes: 3

Views: 814

Answers (1)

Ted Lyngmo
Ted Lyngmo

Reputation: 117503

You need to wait until the child is finished.

exit_code - "Get the exit_code. The return value is without any meaning if the child wasn't waited for or if it was terminated."

#include "boost/process.hpp"

#include <iostream>

std::pair<int, std::string> runCommandAndGetOutput(
    const std::string& cmd, const std::vector<std::string>& args) {
    namespace bp = boost::process;
    boost::asio::io_context ioc;
    std::future<std::string> data;
    bp::child c(cmd, bp::args(args), bp::std_in.close(), bp::std_out > data,
                bp::std_err > bp::null, ioc);
    ioc.run();
    
    c.wait();                        // <---- here

    if(c.exit_code()) {
        std::cerr << "Command '" << cmd
                  << "' failed with return value: " << c.exit_code();
    }
    return {c.exit_code(), data.get()};
}

int main() {
    auto rv = runCommandAndGetOutput("/usr/bin/free", {});

    std::cout << "\n----------\n" << rv.second << '\n' << rv.first << '\n';
}

Upvotes: 3

Related Questions