C++ 获取C中后台shell命令的PID
我想在一个C程序中执行一个Linux命令,并获得它的PID和输出 例如,在下面的C++ 获取C中后台shell命令的PID,c++,linux,shell,C++,Linux,Shell,我想在一个C程序中执行一个Linux命令,并获得它的PID和输出 例如,在下面的executeShellCommand()函数中,如何访问已启动进程的PID commandTest.cpp 运行时使用: /commandTest.“/test.sh&echo$!” 使用命令echo$,其中包含最近创建的后台进程的PID,然后读取它 FILE *p = popen("some command & echo $!"); int pid; fscanf(p, "%d", &pid);
executeShellCommand()
函数中,如何访问已启动进程的PID
commandTest.cpp
运行时使用:
/commandTest.“/test.sh&echo$!”
使用命令echo$代码>,其中包含最近创建的后台进程的PID,然后读取它
FILE *p = popen("some command & echo $!");
int pid;
fscanf(p, "%d", &pid);
pclose(p);
您可以尝试以下方法:
#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#include//for std::hash
#包括
使用名称空间std;
//执行shell命令并返回其输出
字符串executeShellCommand(const char*cmd)
{
字符缓冲区[128];
字符串结果=”;
文件*pipe=popen(cmd,“r”);
如果(!pipe)抛出运行时错误(“popen()失败!”);
试一试{
而(!feof(管道)){
if(fgets(缓冲区,128,管道)!=NULL)
结果+=缓冲区;
}
}捕获(…){
pclose(管道);
投掷;
}
pclose(管道);
//从结果字符串中删除新行字符
int pos;
while((pos=result.find('\n'))!=string::npos)
结果:擦除(pos);
返回结果;
}
int executeShellCommandPid(常量字符*cmd)
{
字符串localCommand(cmd);
//使用输入命令和当前时间生成文件名
时间与时间;
结构tm*时间信息;
字符缓冲区[80];
时间(&rawtime);
timeinfo=localtime(&rawtime);
strftime(buffer,sizeof(buffer),%d-%m-%Y%I:%m:%S”,timeinfo);
字符串当前时间(缓冲区);
散列哈希器;
字符串文件名=to_字符串(哈希器(localCommand+currentTime));
localCommand=localCommand+“&echo$!| grep-w-o-E'[0-9]*'>”+fileName+“.pid”;
系统(localCommand.c_str());
localCommand=“cat”+文件名+”.pid”;
intpid=stoi(executeShellCommand(localCommand.c_str());
localCommand=“rm-rf”+文件名+”.pid”;
系统(localCommand.c_str());
返回pid;
}
int main(int argc,char*argv[])
{
//变量定义
字符串ARG_COM=argv[1];
没问题。到目前为止你做了什么?什么不起作用了?man 3 posix_spawn
你可以从上一个后台进程的shell ID进行回送。echo$!;
或当前进程的IDecho$$;
你提到的popen()
不起作用,你犯了什么错误?。在POSIX中可以通过多种方式获得进程的pid。看看这个@致命-getpid()
不是这里需要的-对于孩子的id,我们需要保存fork()
的结果。我认为这意味着重新实现足够多的popen
。
FILE *p = popen("some command & echo $!");
int pid;
fscanf(p, "%d", &pid);
pclose(p);
#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;
}