PDA

View Full Version : Execute system command from QProcess



graciano
19th February 2014, 11:07
Hi,
I was writing a small piece of code to execute some system commands.
I'm using Debian, i changed GDM3 to allow root access (no security issues needed in this case) and the code is:


void criarWindowsDialog::on_pushButton_2_clicked()
{
ui->commandTextEdit->append("Processo iniciado ...");
QProcess processo;
QString comando;
QStringList args;
QString fullCommand;
QString stdOut;
QString stdError;

//comando 1 - limpar a pasta
comando = "rm";
args.clear();
args<<"-f"<<"/home/srp/imagens/windows/*";
fullCommand = comando;
for(int i = 0; i < args.count(); i++)
fullCommand += " " + args.at(i);
processo.start(comando,args,QIODevice::ReadOnly);
processo.waitForFinished();
stdOut = processo.readAllStandardOutput();
stdError = processo.readAllStandardError();
ui->commandTextEdit->append(fullCommand);
ui->stdOutTextEdit->append(stdOut);
ui->stdErrorTextEdit->append(stdError);


When i check the message from "stdError" i get:

Failed to open terminal.TERM environment variable needs set.

This is a simple command to delete files inside a folder.
Is the problem related to configuration in debian?

anda_skoa
19th February 2014, 12:33
Hmm, strange, rm does't sounds like a program that needs a terminal.

Anyway, set the TERM environment variable as suggested, see QProcess::setProcessEnvironment().

In that particular case, removing files, you could also do that directly.

Cheers,
_

graciano
19th February 2014, 16:19
The same thing happens when i try ...


comando = "partimage";
args.clear();
args<<"-z1"<<"-o"<<"-d"<<"-b"<<"-save"<<"/dev/sda1 /home/srp/imagens/windows/windows.gz";
fullCommand = comando;
for(int i = 0; i < args.count(); i++)
fullCommand += " " + args.at(i);
processo.start(comando,args,QIODevice::ReadOnly);
processo.waitForFinished();
stdOut = processo.readAllStandardOutput();
stdError = processo.readAllStandardError();
ui->commandTextEdit->append(fullCommand);
ui->stdOutTextEdit->append(stdOut);
ui->stdErrorTextEdit->append(stdError);


... only now i'm executing "partimage".
I used QProcess::setProcessEnvironment() to set TERM=xterm but i get the same errors.

By the way, the QProcess::setProcessEnvironment() is labeled as depretated. What does it mean? Not to use??

anda_skoa
19th February 2014, 19:50
setEnvironment() is deprecated, setProcessEnvironment is not.

Try to execute something simple that evaluates the TERM variable, e.g.

echo $TERM

Does this give the correct result?

Cheers,
_

graciano
20th February 2014, 08:38
I tried this:


env.insert("TERM", "xterm");
processo.setProcessEnvironment(env);

//comando 1 - limpar a pasta
comando = "echo";
args.clear();
args<<"$TERM";
fullCommand = comando;
for(int i = 0; i < args.count(); i++)
fullCommand += " " + args.at(i);
processo.start(comando,args,QIODevice::ReadOnly);
processo.waitForFinished();
stdOut = processo.readAllStandardOutput();
stdError = processo.readAllStandardError();
ui->commandTextEdit->append(fullCommand);
ui->stdOutTextEdit->append(stdOut);
ui->stdErrorTextEdit->append(stdError);


Using a system console i get:


root@srp:~# echo $TERM
xterm


Using my code, in the ui->stdOutTextEdit e read:


$TERM

anda_skoa
20th February 2014, 09:20
Right, sorry, echo $TERM won't work because there is no shell expanding $TERM.

You could try with a simple shell script as the command, no args needed



#!/bin/sh
echo $TERM


Cheers,
_

graciano
20th February 2014, 10:05
Using a shell script i get the expected output:


comando = "bash";
args.clear();
args<<"/home/srp/imagens/scripts/criarwindows.sh";


Where does it lead us?