PDA

View Full Version : Killing QProcess shell script



mister_m
18th August 2016, 16:14
Hey all,

First time poster on these forums. Quick question.

I'm running a simple shell command line from QProcess; Both the shell and application i'm running are designed to close on a interrupt signal. However, I'm having the shell application lingering around in system monitor after i call QProcess->Close() and QApplication::quit(). I'm guessing it has to do with how QT sends signals.

Some code.


QString rx_path = QCoreApplication::applicationDirPath();
command = "trap 'exit;' 2; cd " + rx_path + "/applications/bin/ && ./application -udp 42003";
rx_process = new QProcess;
rx_process->start("sh",QStringList() << "-c" << command);


trap 2 = SIGINT

This launches application with the correct flags, but i'll still see application in system monitor, many instances of it for every time i run my QT application.
I've also tried "trap 'exit;' INT; . . . ." and "trap 'exit;' SIGINT . . . " SIGINT returns bad trap. IIRC close() sends SIGINT. I've also tried kill() and terminate().

Kinda running out of ideas, maybe i'm doing something wrong. Is there a way to manually send Ctrl+C to my sh?

If i run "trap 'exit;' 2; cd " + rx_path + "/applications/bin/ && ./application -udp 42003" from terminal it exits out of both my application and the shell properly.

anda_skoa
19th August 2016, 10:21
You can send signals to the process using the C signal API and the processId() from the QProcess.

But can I ask if you really need to run this in a shell? Have you tried running the program directly?

Cheers,
_

mister_m
19th August 2016, 15:20
Thank you for the reply.

I've tried using signal.h's int kill (pid_t pid, int signum) where PID = qprocess->pid(). However that returns that the signal wasn’t sent.

So about using SH, i tried running without sh to start however i had no luck passings the flags to the program. I might have been doing it wrong however. Using shell i could get around this but that brought up the non-terminating process problem.



QString rx_path = QCoreApplication::applicationDirPath();
QString command = ". " + rx_path + "/applications/bin/ && ./application";
QString flags = " -udp 42003"
rx_process = new QProcess;
rx_process->start(command,QStringList() << flags);


I've also tried a variant of this passing the flags into command and leaving args empty but that doesnt work either.

I agree that the right solution here would to not use sh.

anda_skoa
19th August 2016, 15:38
Ah.

You need to pass the two commandline args as separate strings in the arguments stringlist.
I.e. "-udp" and "42003"

Otherwise they will be passed as one argument.

Also, the first part of your command is actually changing the process working directory, see QProcess::setWorkingDirectory().
The command would only be the application executable, probably best with full path.

Cheers,
_

mister_m
19th August 2016, 16:00
Thanks for getting back to me,

Just so I understand you right, here's what i've done. (Still not running my application :S)

w/o changing working directory


QString rx_path = QCoreApplication::applicationDirPath();
QString command = ". " + rx_path + "/applications/bin/application";
rx_process = new QProcess;
rx_process->start(command,QStringList() << "-udp" << "42003");


w/ changing working directory


QString rx_path = QCoreApplication::applicationDirPath();
QString command = "./application";
rx_process = new QProcess;
rx_process->setWorkingDirectory(rx+path + "/applications/bin/");
rx_process->start(command,QStringList() << "-udp" << "42003");

anda_skoa
19th August 2016, 16:31
No "." in your commands.
That is only needed by the shell since the current directory is not in $PATH.

So more something like


QString rx_path = QCoreApplication::applicationDirPath();
QString bin_path = rx_path + "/applications/bin";
QString command = bin_path + "/application";
rx_process = new QProcess;
rx_process->setWorkingDirectory(bin_path);
rx_process->start(command,QStringList() << "-udp" << "42003");


Cheers,
_

mister_m
19th August 2016, 17:08
Wow thank you anda_skoa.

Cant believe it was as simple as leaving out the ".", and splitting up the params in my QStringList. Solved all my issues :)