Results 1 to 7 of 7

Thread: Killing QProcess shell script

  1. #1
    Join Date
    Aug 2016
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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.
    Qt Code:
    1. QString rx_path = QCoreApplication::applicationDirPath();
    2. command = "trap 'exit;' 2; cd " + rx_path + "/applications/bin/ && ./application -udp 42003";
    3. rx_process = new QProcess;
    4. rx_process->start("sh",QStringList() << "-c" << command);
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by mister_m; 18th August 2016 at 17:33.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  3. #3
    Join Date
    Aug 2016
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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.

    Qt Code:
    1. QString rx_path = QCoreApplication::applicationDirPath();
    2. QString command = ". " + rx_path + "/applications/bin/ && ./application";
    3. QString flags = " -udp 42003"
    4. rx_process = new QProcess;
    5. rx_process->start(command,QStringList() << flags);
    To copy to clipboard, switch view to plain text mode 

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  5. #5
    Join Date
    Aug 2016
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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
    Qt Code:
    1. QString rx_path = QCoreApplication::applicationDirPath();
    2. QString command = ". " + rx_path + "/applications/bin/application";
    3. rx_process = new QProcess;
    4. rx_process->start(command,QStringList() << "-udp" << "42003");
    To copy to clipboard, switch view to plain text mode 

    w/ changing working directory
    Qt Code:
    1. QString rx_path = QCoreApplication::applicationDirPath();
    2. QString command = "./application";
    3. rx_process = new QProcess;
    4. rx_process->setWorkingDirectory(rx+path + "/applications/bin/");
    5. rx_process->start(command,QStringList() << "-udp" << "42003");
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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
    Qt Code:
    1. QString rx_path = QCoreApplication::applicationDirPath();
    2. QString bin_path = rx_path + "/applications/bin";
    3. QString command = bin_path + "/application";
    4. rx_process = new QProcess;
    5. rx_process->setWorkingDirectory(bin_path);
    6. rx_process->start(command,QStringList() << "-udp" << "42003");
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    mister_m (19th August 2016)

  8. #7
    Join Date
    Aug 2016
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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

Similar Threads

  1. Replies: 1
    Last Post: 18th November 2014, 18:18
  2. Replies: 4
    Last Post: 22nd April 2014, 15:34
  3. Killing programs started with QProcess
    By AndresBarbaRoja in forum Newbie
    Replies: 0
    Last Post: 1st June 2011, 08:12
  4. QProcess: how to run shell script with password?
    By cutie.monkey in forum Qt Programming
    Replies: 0
    Last Post: 23rd March 2010, 05:43
  5. Call script shell with Qt
    By zeeb100 in forum Qt Programming
    Replies: 6
    Last Post: 26th February 2009, 09:17

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.