- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想从 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 $!"
最佳答案
你可以试试这个:
#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;
}
关于c++ - 获取 C 中后台 shell 命令的 PID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45918306/
manpage of reboot()说 Behavior inside PID namespaces Since Linux 3.4, if reboot() is called from a PI
我今天第一次在 Nvidia jetson Xavier 中安装和配置并使用了电视, 但重启后我遇到了这个问题: teamviewerd.service: 启动后无法打开 PID 文件/var/run
我在我的服务器上安装了 hhvm,在我重新启动服务器之前它一直运行良好。在 hhvm 的日志中,我看到了这个错误: Unable to read pid file /var/run/hhvm/pid
我正在尝试为我的应用程序精简,但随后无法生成 pid: $ thin -C /var/www/project_path/current/config/myproject.testing.yml sta
我正在从我的私有(private) git 存储库安装应用程序。我安装了所有依赖项并且我正在使用 Capistrano。我能够在我的本地计算机上成功运行应用程序。我正在使用 rails -v 3.2.
我已按照 DigitalOcean 指南中的步骤进行操作 here和 here使用 nginx 和 Unicorn 设置 Sinatra 服务器。我在倒数第二步: start the Unicorn
我在 C 程序中连续进行了 3 个 fork 。 1.它会以相同的顺序执行吗? (我的猜测是肯定的)。 2. 如果我做 pgrep myexecutable从 shell 中,它会按照启动的顺序给出进
我尝试通过FT_Prog更改FTDI芯片(R232R)中的PID。它可以工作,但之后我发现 Windows 7 自动重新安装 USB 设备的驱动程序,而不是 FTDI 设备。所以我想将PID改回默认值
第一次在这里发表 简单情况:在 PUTTY 中,我必须创建一个名为 admin.pid 的文件,当用户启动我正在创建的“应用程序”时,它会在其中存储 PID。我怎样才能做到这一点?谢谢 最佳答案 使用
我设法为每个单独的进程输出正确的进程 ID 顺序,但我的问题是我无法显示子进程的 PID。 我的程序能够打印 parent 的 PID 和孙子的 PID。我确实看到了 child 的 PID,但它显示
我正在从事一个项目,其中有许多 PID,我必须找出其中哪些是僵尸进程,然后终止它们的父进程以终止初始僵尸进程。我不确定是否有任何方法可以找出给定 PID 的 PPID 是什么。任何帮助将不胜感激。 最
我正在使用 htop,所以看看哪些进程占用了大量内存,以便我可以杀死它们。我有很多 tmux session 和很多类似的过程。如何检查 PID 所在的 tmux Pane ,以便确定我正在杀死我想杀
我正在通过运行跟踪应用程序: strace -f -y -qq -z -etrace=execve,... -o app.trace ./app 有没有办法确定哪个进程产生了哪个 child_proc
在我使用 exec 之后docker 容器内的命令我可以使用 exec inspect 获取 PID .问题是这个 ID 不是容器本地的,而是系统 PID。所以我会得到类似 22620 的东西,而 d
我有一个我开发的用于启动 Java 程序的 System V 初始化脚本。由于某种原因,无论何时创建 PID 文件,它都包含多个 PID 而不是一个。 下面是启动服务并写入PID文件的相关代码: da
我有一个变量 pidfile,它存储进程的 PID。 如何使用 Ruby 以编程方式终止 pidfile 中的 pid,假设我只知道文件名,而不是其中的实际 PID。 最佳答案 Process.kil
我读入了Beej's fork() primer当我调用 pid = fork(); 时,父进程获取子进程的 pid,而在子进程内部 pid = 0。 现在,由于子进程开始执行 在 fork() 语句
我正在尝试从另一个 Python 脚本运行一个 Python 脚本,并获取它的 pid 以便稍后可以终止它。 我尝试使用参数 shell=True' 的 subprocess.Popen(),但是pi
我有一个用 Cygwin 生成的进程shell 脚本,我无法用 kill 杀死它命令。即使与 Cygwin kill与 -f选项,我收到此消息: kill: couldn't open pid 123
我尝试在我的模型中为阀门构建一个 PID Controller ,我计划进行一些过程识别,获得系统对阶跃脉冲的响应和系统的传递函数,然后我可以设计 PID Controller 。但我不确定是否有用于
我是一名优秀的程序员,十分优秀!