Process launching, and killing
-
Hello, I have to do the following but can't get any good way to start with. The application is to be developed for Mac and has to:
- Check if an application, say named "A", is running or not
- If it is running, the application has to terminate it
- If it is not running, I have to launch it
I have done this in Windows very easily using windows api, but in Mac using Qt, i don't have any help..
I couldn't get any help online and after all searching, I am posting this here.
Kashif
-
You must use MAC specific APIs, Qt does not give that to you.
You can start processes with QProcess, but monitoring already running processes and killing them, has to be done in a platform dependent way. -
Thanks Gerolf for your reply...
Actually I am using Qt Creator to do all my work, and don't know how to proceed towards what you are mentioning.. Can you guide me to achieve my objective?
Kashif
-
QtCreator is just an IDE.
You have to use the MAC APIs for that.
But I am not a MAC user/developer, sorry. -
Hi Kashif,
you are looking for information out of the kernel. On Linux you find this in /proc. On Mac I'm not 100% sure, but the corresponding interface is called "sysctl". Check out the "manpagez":http://www.manpagez.com/man/3/sysctl/. If you are lucky you find example code in "Amit Singh's book":http://osxbook.com/
-
If you know the path of the application, just call QDesktopServices::openUrl on it:
@
QString appPath = "/Applications/Qt Creator.app";
QDesktopServices::openUrl(QUrl::fromLocalFile(appPath));
@It opens the application if it's not running. If it's already running, it brings it to the foreground.
If you want to have the application stay in the background, you can use this snippet:
@
QStringList args;
args << "-g"; // let the app stay in the background
args << "/Applications/Qt Creator.app";
int ok = QProcess::execute(cmd, args);
qDebug() << "ok=" << ok;
@ -
Thanks, Volker, nice tip! I wasn't aware that openUrl() could do that;)
-
Oh, I missed one requirement to close a process.
Have a look at this "article on stackoverflow":http://stackoverflow.com/questions/2518160/programmatically-check-if-a-process-is-running-on-mac and try a google search on "mac check if application is running".
@unclewerner: A nice side effect of the "open" command on the mac, it can handle almost everything. I'm missing it badly on my linux boxes....