Jörg Bornemann

A new QProcess::startDetached

Published Friday August 25th, 2017
5 Comments on A new QProcess::startDetached
Posted in C++, Dev Loop, Qt

From Qt 5.10 on, there is a new way how to start detached processes with QProcess.

Of course you know this, but let me quickly repeat what a detached process is. If you start a program using QProcess without detaching, then the destructor of QProcess will terminate the process. In contrast, a detached process keeps running unaffected when the calling process exits. On Unix, a detached process will run in its own session and act like a daemon.

Traditionally, we start detached processes with the static QProcess::startDetached() method.

QProcess::startDetached("aplay tada.wav");

There is a second overload that supports passing a separate argument list and the working directory. It also lets us retrieve the PID of the started process.

qint64 pid;
QProcess::startDetached("mpg123", {"Jeopardy_Theme.mp3"}, musicDirPath, &pid);
printf("Performing a lengthy calculation...");
QProcess::startDetached("kill", {QString::number(pid)});

This little example crunches numbers for a while and plays the Jeopardy Theme to entertain the user. When the calculation result is ready, it kills the music player using the obtained PID.

When running this example on Linux, you will notice a little annoyance: we have no way of suppressing the output of the detached process.
(Okay, this particular tool has the “–quiet” option, but let’s ignore that for the example’s sake.)

Qt Users have requested for a long long time the ability to

  • set the process environment,
  • redirect stdin, stdout, stderr to files
  • and set native arguments and the CreateProcess argument modifier on Windows

for detached processes.

All this is now possible in Qt 5.10! But instead of adding a monstrous overload for the static startDetached() method, we added a non-static QProcess::startDetached(qint64 *pid). This new member function reads the properties of your QProcess object and starts the to-be-detached process accordingly. It can be used as follows:

QProcess process;
qint64 pid;

The redirection to /dev/null makes sure that the user is not disturbed by visual output of the audio player.

Only a certain sensible subset of functions is supported for startDetached():

  • setArguments()
  • setCreateProcessArgumentsModifier()
  • setNativeArguments()
  • setProcessEnvironment()
  • setProgram()
  • setStandardErrorFile()
  • setStandardInputFile()
  • setStandardOutputFile()
  • setWorkingDirectory()

All other properties of the QProcess object are ignored.

If there are more properties to be supported in the future, they can be easily added in the implementation of the new startDetached().

Happy detaching!

Do you like this? Share it
Share on LinkedInGoogle+Share on FacebookTweet about this on Twitter

Posted in C++, Dev Loop, Qt


Thiago Macieira says:

Please note the race condition in killing the PID you got: what happens if the process you started has already exited and the PID was recycled to a different process?

So if you’re going to use the PID of the launched process, make sure it’s still the same. How? That’s an exercise left to the reader.

Jörg Bornemann Jörg Bornemann says:

The solution, of course, is to play the 10 hours version of anything.

Joking aside, the race condition is real, and there are much better ways to actually play sound in the background. This is a contrived example. Please don’t do this at home.

Jan D says:

Maybe add “–loop -1” to the arguments of mpg123 to appease the nitpickers 😉

Ahmed arif says:

check for “ps aux | grep ….” stuff using Qprocess … don’t complicate your life !

angelus says:

is good!

Commenting closed.

Get started today with Qt Download now