我想在C程序中执行Linux命令并获取其PID及其输出。
例如,在下面的executeShellCommand()
函数中,如何访问已启动的进程的PID?
#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;
}
#!/bin/bash
while [[ 1 ]]
do
sleep 1
done
使用以下命令运行:
./commandTest "./test.sh & echo $!"
答案 0 :(得分:4)
你可以试试这个:
#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;
}
答案 1 :(得分:1)
命令echo $!
,其中包含创建的最新后台进程的PID,然后读取它。
FILE *p = popen("some command & echo $!");
int pid;
fscanf(p, "%d", &pid);
pclose(p);