Razavi Razavi - 2 months ago 68
C Question

Get PID of a backgrouded shell command in C

I want to execute a Linux shell command at background in a C program and get it's PID, but conventional system() function in C does not return PID.

popen() function and similar solutions don't work when I put the shell command in background.

For example : commandTest.cpp

#include<unistd.h>
#include<fstream>
#include<string.h>
#include<iostream>
#include<string>
#include<cstdlib>
#include <stdexcept>

using namespace std;

// execute a shell command and return its output
string executeShellCommand(const char* cmd)
{
char buffer[128];
string result = "";
FILE* pipe = popen(cmd, "r";
if (!pipe) throw runtime_error("popen() failed!";
try {
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
} catch (...) {
pclose(pipe);
throw;
}
pclose(pipe);
return result;
}

int main(int argc, char* argv[])
{
string ARG_COM = argv[1];
cout << "executeShellCommand : " << executeShellCommand(ARG_COM.c_str()) << endl;
return 0;
}


to run this script: test.sh

#!/bin/bash
while [[ 1 ]]
do
sleep 1
done


finally run with :

./commandTest "./test.sh & echo $!"

Answer Source

you can try this:

#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<fstream>
#include<string.h>
#include<ctime>
#include<iostream>
#include<string>
#include<cstdlib>
#include<sys/time.h>
#include<signal.h>
#include <stdexcept>
#include <functional> //for std::hash
#include <ctime>

using namespace std;

// execute a shell command and return its output
string executeShellCommand(const char* cmd) 
{
    char buffer[128];
    string result = "";
    FILE* pipe = popen(cmd, "r";
    if (!pipe) throw runtime_error("popen() failed!";
    try {
        while (!feof(pipe)) {
            if (fgets(buffer, 128, pipe) != NULL)
                result += buffer;
        }
    } catch (...) {
        pclose(pipe);
        throw;
    }
    pclose(pipe);

// remove new line character from result string
int pos;
while((pos=result.find('\n')) != string::npos)
                    result.erase(pos);
    return result;
}

int executeShellCommandPid(const char* cmd) 
{
string localCommand(cmd);

// make a file name using the input command and the current time
time_t rawtime;
struct tm * timeinfo;
char buffer[80];    
time (&rawtime);
timeinfo = localtime(&rawtime); 
strftime(buffer,sizeof(buffer),"%d-%m-%Y %I:%M:%S",timeinfo);
string currentTime(buffer); 
hash<string> hasher;
    string fileName = to_string(hasher(localCommand+currentTime)); 

localCommand = localCommand + " & echo $! | grep -w -o -E '[0-9]*'>" + fileName + ".pid";
system(localCommand.c_str());
localCommand = "cat " + fileName + ".pid";
int pid = stoi(executeShellCommand(localCommand.c_str()));
localCommand = "rm -rf " + fileName + ".pid";
system(localCommand.c_str());
return pid;
}

int main(int argc, char* argv[])
{
// variable definitions
string ARG_COM = argv[1];

cout << "system : " << system(ARG_COM.c_str()) << endl;
//cout << "executeShellCommand : " << executeShellCommand(ARG_COM.c_str()) << endl;
//cout << "executeShellCommandPid : " << executeShellCommandPid(ARG_COM.c_str()) << endl;

return 0;

}