2

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.

Example: get output of all processes using 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"

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 and reading its output. This example is getting all pid's of all running chrome processes

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.

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?

6
  • Do you need a shell for ps -aux? Why not just use ps as the command and -aux as the argument? Commented Nov 3, 2016 at 17:09
  • @KevinKrammer I need to send complete shell commands and read output, as the one described above, another example is ps -aux | grep chrome | tr -s " " | cut -d " " -f 2, this will retrieve all the pid's of all chrome processes. I need to send commands like the one I mentioned and retrieve its output. Commented Nov 3, 2016 at 17:27
  • Wouldn't it be easier to just call the ps command and then read and process its output? Commented Nov 3, 2016 at 17:30
  • @KevinKrammer you seem to be persistent on the idea of an alternative for a specific command. I need to send ANY command to the shell and read the response. How will I process this then ifconfig | grep tun0 > /dev/null;r=$?;echo $r. It is exactly the same as the previous. Do you understand now? Commented Nov 3, 2016 at 17:43
  • "any command" as in the user types those? Why would the user not just use a terminal then? Commented Nov 4, 2016 at 14:00

1 Answer 1

3

you need to use waitForFinished() after start, not 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 ...

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

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 .. ";
Sign up to request clarification or add additional context in comments.

2 Comments

Quickly read up on the waitForFinished(), it makes sense to have it before, why does it need to be after?
@KGCybeX you welcome ! .. actually it makes sense to 1-start 2-wait to start 3-wait to finish ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.