-1

I want the program to check if the script already started and if not - then start it (in Terminal and as root)

Thank you! ;-)

QProcess *proc = new QProcess();
     proc->setWorkingDirectory("/home/user/Documents/");
     proc->start("/home/user/Documents/script.sh");

     delete proc;
user3027198
  • 183
  • 3
  • 11

1 Answers1

2

QProcess runs the external script asyncronously, so by doing

proc->start(...);
delete proc;

You kill it as soon as it starts (or maybe even before). Try adding waitForFinished() before the delete.

proc->start(...);
proc->waitForFinished(); 
delete proc;
David
  • 1,510
  • 14
  • 20
  • Thank you! But how to check if the script is already running? – user3027198 Apr 24 '16 at 12:16
  • and is it necessary to `delete proc` even if I have **exit** command at the end of my script? – user3027198 Apr 24 '16 at 12:29
  • 1
    Is it only your program that will start the script? If so then the state() function of the QProcess will tell you if its running or not. And yes, you will need to delete the QProcess once its finished. When your script ends the QProcess will still contain the input and output streams it used and any other state that it holds until it is deleted. This is a good thing, otherwise once the script ended you couldnt access any data it output. – David Apr 24 '16 at 12:45
  • Thank you very much! But how to write something like this: `if (proc->state()==NotRunning) {do smoething}` – user3027198 Apr 24 '16 at 12:59
  • 1
    pretty much, but NotRunning is an enum value in QProcess so you need to compare state() to QProcess::NotRunning – David Apr 24 '16 at 13:06