Killing QProcess shell script
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.
Code:
command = "trap 'exit;' 2; cd " + rx_path + "/applications/bin/ && ./application -udp 42003";
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.
Re: Killing QProcess shell script
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,
_
Re: Killing QProcess shell script
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.
Code:
QString command
= ". " + rx_path
+ "/applications/bin/ && ./application";
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.
Re: Killing QProcess shell script
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,
_
Re: Killing QProcess shell script
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
Code:
QString command
= ". " + rx_path
+ "/applications/bin/application";
rx_process
->start
(command,
QStringList() <<
"-udp" <<
"42003");
w/ changing working directory
Code:
rx_process->setWorkingDirectory(rx+path + "/applications/bin/");
rx_process
->start
(command,
QStringList() <<
"-udp" <<
"42003");
Re: Killing QProcess shell script
No "." in your commands.
That is only needed by the shell since the current directory is not in $PATH.
So more something like
Code:
QString bin_path
= rx_path
+ "/applications/bin";
QString command
= bin_path
+ "/application";
rx_process->setWorkingDirectory(bin_path);
rx_process
->start
(command,
QStringList() <<
"-udp" <<
"42003");
Cheers,
_
Re: Killing QProcess shell script
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 :)