简体   繁体   English

qt通过qprocess运行shell命令

[英]qt run shell commands via qprocess

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 2 我需要发送特定的完整命令,我不希望找到缩短的命令或替代命令,我需要一种发送命令的方法,例如: ps -aux | grep chrome | tr -s " " | cut -d " " -f 2 ps -aux | grep chrome | tr -s " " | cut -d " " -f 2 ps -aux | grep chrome | tr -s " " | cut -d " " -f 2 and reading its output. ps -aux | grep chrome | tr -s " " | cut -d " " -f 2并读取其输出。 This example is getting all pid's of all running chrome processes 此示例获取所有正在运行的chrome进程的所有pid

Interpreters attempted : 口译员尝试

  • 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. 有人建议在SO上的帖子中使用此方法来运行Shell代码,但无法再次找到它。

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? 有什么建议吗?

you need to use waitForFinished() after start , not before 您需要在start之后而不是之前使用waitForFinished()

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 waitForFinished timed 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 .. ";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM