在C中获取后台shell命令的PID

我想从C程序中执行一个Linux命令,并获得它的PID以及它的输出。

例如在下面的executeShellCommand()函数中,如何访问启动的进程的PID?

commandTest.cpp

 #include <unistd.h> #include <cstdio> #include <iostream> #include <string> #include <stdexcept> // execute a shell command and return its output std::string executeShellCommand(const char* cmd) { char buffer[128]; std::string result = ""; FILE* pipe = popen(cmd, "r"); if (!pipe) throw std::runtime_error("popen() failed!"; try { while (!std::feof(pipe)) { if (std::fgets(buffer, sizeof buffer, pipe)) result += buffer; } } catch (...) { pclose(pipe); throw; } pclose(pipe); return result; } int main(int argc, char* argv[]) { std::cout << "executeShellCommand : " << executeShellCommand(argv[1]) << std::endl; return 0; } 

test.sh

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

运行:

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

你可以试试这个:

 #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; } 

有命令echo $! ,其中包含最近创建的后台进程的PID,然后读取它。

 FILE *p = popen("some command & echo $!"); int pid; fscanf(p, "%d", &pid); pclose(p);