mercredi 24 avril 2019

Capture output from console app started by daemon

How is it possible to capture the output from a console app, if it's started by a daemon process in Ubuntu Server 18.04? For a non-daemon process I usually use this:

string myexec(const char* cmd) 
{
    array<char, 128> buffer;
    string result = "";
    unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
    if (!pipe) 
    {
        result = "FAIL";
        throw std::runtime_error("popen() failed!");
    }
    while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) 
    {
        result += buffer.data();
    }
    return result;
}

Running this, as is, from the daemon doesn't fail, just returns empty.

Aucun commentaire:

Enregistrer un commentaire