3

After starting a process with QProcess::startDetached, how can I stop it later?

Say the main program runs, then starts the detached process, which runs independently. The user closes the main program, then later opens it up again and wants to stop the process. How would I find the process and then stop it?

László Papp
  • 51,870
  • 39
  • 111
  • 135
JVE999
  • 3,327
  • 10
  • 54
  • 89
  • 2
    Only by using platform-dependent means to locate the process, and then issue a command to kill it. After a process starts as detached, and additionally your Qt program exits, you have no control over that process via QProcess. – DNT May 26 '14 at 00:37
  • In that case my solution will be to write a file to manage processes that each one reads. Thanks – JVE999 May 26 '14 at 00:40
  • Is there a way I could prevent the application from the same process twice? I thought of using a file, but if the phone is shut off or the process crashes, the file will still say it's running and won't let it start again. – JVE999 May 26 '14 at 01:03
  • I did find a a method. It's a bit contrived. I first start the detached process that generates a unique id. That process write to a file whenever it runs (was a 1 minute timer). When it runs, it writes its id to a file. Then, if there happens to be another one that ran, if it sees a previous one ran, it just writes its id to the file and doesn't run, then, when the next one runs, it sees if its id is already in the file and if it is, it shuts itself off and clears the file, then the next run ends up running freely, being the only one running. This may end up skipping some time. – JVE999 Jul 20 '14 at 21:46
  • You can add a timestamp, too, as that might indicate it wasn't run recently and help with deciding whether or not to shut it down. The issue was if I just write the id to a file, when I turn the phone off, the file will say it's still running. The same applies to if it crashes. – JVE999 Jul 20 '14 at 21:47

2 Answers2

1

Is there a way I could prevent the application from the same process twice?

No, it will be decoupled from your application. You could get the the PID of it and then send a SIGSTOP on Linux, but this is platform specific and will not work without POSIX support, like with msvc. You would need to hand-craft your version therein.

Is there a way I could prevent the application from the same process twice?

Yes, by using lock file in the detached process. If that detached process happens to be written in at least partially Qt, you could use the QLockFile class.

If you happen to detach some platform specific process, then you have the same recurring issue again, for sure.

László Papp
  • 51,870
  • 39
  • 111
  • 135
  • This is a cleaner solution than the one I found, this solution being to simply lock a file indefinitely when the process starts, and when the main program tries to start another process, it checks to see if the other one is locked. However, this could result in misidentifying the file as locked in the event it had crashed recently or perhaps the phone restarted (not sure if it unlocks it in this case) and it hadn't been declared stale and unlocked yet, so it's a bit less reliable and could end with unhappy users. – JVE999 Jul 20 '14 at 21:55
  • @JVE999: I do not follow. The lockfile does get stale automatically after 30 seconds by default. – László Papp Jul 21 '14 at 00:45
  • I didn't notice. Well, that's a good solution then. I would add to set a 31 second timer before attempting to start the process to be on the safe side. If for some reason the process just crashed while the timer was running (it shouldn't of course), it won't start, but otherwise that should work. – JVE999 Jul 22 '14 at 01:55
0

Here's the answer I figured out:

I first start the detached process that generates a unique id. That process write to a file whenever it runs (was a 1 minute timer). When it runs, it writes its id to a file. Then, if there happens to be another one that ran, if it sees a previous one ran, it just writes its id to the file and doesn't run, then, when the next one runs, it sees if its id is already in the file and if it is, it shuts itself off and clears the file, then the next run ends up running freely, being the only one running. This may end up skipping some time.

You can add a timestamp, too, as that might indicate it wasn't run recently and help with deciding whether or not to shut it down. The issue was if I just write the id to a file, when I turn the phone off, the file will say it's still running. The same applies to if it crashes.

JVE999
  • 3,327
  • 10
  • 54
  • 89
  • I'm going to leave it up for comments for a bit as there may be a better one. – JVE999 Jul 20 '14 at 21:55
  • I miss the point what this answer is trying to achieve. The PID is already unique, and it is put into the file by QLockFile. The lock file also gets stale automatically in 30 seconds by default, so it seems to me that you are reinventing the wheel and basically what I already wrote about four months ago. – László Papp Jul 21 '14 at 00:47