bash qt 通过 qprocess 运行 shell 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40407394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:21:51  来源:igfitidea点击:

qt run shell commands via qprocess

c++linuxbashqtshell

提问by KGCybeX

I am developing a small QT application to interact with the terminal, to send commands to the terminal and to read back information printed out.

我正在开发一个小型 QT 应用程序来与终端交互,向终端发送命令并读回打印出来的信息。

Example: get output of all processes using ps -aux

示例:使用获取所有进程的输出 ps -aux

PROBLEM

问题

I am able to write information out to the terminal but I dont think it is in the system scope, actual example:

我能够将信息写出到终端,但我认为它不在系统范围内,实际示例:

Command passed to shell interpreter: "echo "pre"; ps -aux; echo "post"

命令传递给 shell 解释器"echo "pre"; ps -aux; echo "post"

edit from comment:

从评论编辑:

I need to send specific complete commands, I am not looking for shortened or alternative commands, I require a method of sending a command like this : ps -aux | grep chrome | tr -s " " | cut -d " " -f 2and reading its output. This example is getting all pid's of all running chrome processes

我需要发送特定的完整命令,我不是在寻找缩短的或替代的命令,我需要一种发送这样的命令的方法:ps -aux | grep chrome | tr -s " " | cut -d " " -f 2并读取其输出。此示例获取所有正在运行的 chrome 进程的所有 pid

Interpreters attempted:

口译员尝试

  • sh
  • /bin/bash
  • sh
  • /bin/bash

Code:

代码:

QProcess *proc_ovpn = new QProcess(this);
proc_ovpn->waitForFinished();
proc_ovpn->start("sh",QStringList() << "-c" << "echo \"pre\";ps -aux; echo \"post\"");
proc_ovpn->setProcessChannelMode(QProcess::MergedChannels);
QString str(proc_ovpn->readAllStandardOutput());
return str;                             <<< ======= //breakpoint here

Debug information:

调试信息:

When breakpoint is reached, debug information as follows:

到断点时,调试信息如下:

Locals      
    str ""  QString
    this    @0x555555ad7be0 Interface
Inspector       
Expressions     
Return Value        
Tooltip     
    10000000    10000000    int

It was suggested to run shell code using this method above from a post on SO, could not find it again.

建议使用上面的这种方法运行 shell 代码,从 SO 上的一个帖子,找不到了。

I am at a loss, I do not understand why running these commands to not interact directly with the system (and its information),

我不知所措,我不明白为什么运行这些命令不直接与系统(及其信息)交互,

Any advice?

有什么建议吗?

回答by HazemGomaa

you need to use waitForFinished()after start, not before

你需要使用waitForFinished()after start,而不是 before

proc_ovpn->start("sh",QStringList() << "-c" << "echo \"pre\";ps -aux; echo \"post\"");
proc_ovpn->waitForFinished();

Note that waitForFinished()blocks until the process (that has been invoked by start) has finished ...

请注意,waitForFinished()阻塞直到进程(已被调用start)完成...

Also, you may check if the process is started successfully and/or if waitForFinishedtimed out

此外,您可以检查进程是否成功启动和/或是否waitForFinished超时

proc_ovpn->start("sh",QStringList() << "-c" << "echo \"pre\";ps -aux; echo \"post\"");

if(!proc_ovpn->waitForStarted()) //default wait time 30 sec
    qWarning() << " cannot start process ";

int waitTime = 60000 ; //60 sec
if (!proc_ovpn->waitForFinished(waitTime))
         qWarning() << "timeout .. ";