Execute system command from QProcess
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:
Code:
void criarWindowsDialog::on_pushButton_2_clicked()
{
ui->commandTextEdit->append("Processo iniciado ...");
//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:
Code:
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?
Re: Execute system command from QProcess
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,
_
Re: Execute system command from QProcess
The same thing happens when i try ...
Code:
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??
Re: Execute system command from QProcess
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,
_
Re: Execute system command from QProcess
I tried this:
Code:
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:
Code:
root@srp:~# echo $TERM
xterm
Using my code, in the ui->stdOutTextEdit e read:
Re: Execute system command from QProcess
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
Code:
#!/bin/sh
echo $TERM
Cheers,
_
Re: Execute system command from QProcess
Using a shell script i get the expected output:
Code:
comando = "bash";
args.clear();
args<<"/home/srp/imagens/scripts/criarwindows.sh";
Where does it lead us?