Check if an application is running
Hi, i've searched a lot on the internet, but I didn't find any solution to my problem. I'm making kind of a launcher for some applications. I would want to know, when I start my program, if the app it should run when I click on a button (let's say Firefox) is already launched. I tried various things with QProcess but it didn't seem to work.
This is probably OS dependent. If you are working on windows, see the below link.
@Heryon Yea I don't know of anything in Qt to do that. You will have to write platform specific code for each OS you support. It's quite easy on windows and linux, and probably on OSX although I haven't done it on OSX. But since it's a posix OS it should be as easy as linux.
Ok thank you, I'll try that.
@Heryon Do you want to do this on Windows or Linux or a combination of operating systems?
The basic approach is to search the list of active processes for your executable.
For Windows use:
tasklist /fo csv /nh
For Linux use:
ps axco pid,command
You would need to parse the results correctly for the particular OS.
@Heryon For windows you can use this api call https://msdn.microsoft.com/en-us/library/windows/desktop/ms682623(v=vs.85).aspx.
And for linux I would recommend going straight to /proc rather than using ps. Using ps is fine, don't get me wrong, it's just having to allocate and run another process rather than just using the filesystem. Takes a lot more resources and time. There are open source libraries to help you read /proc if you don't want to do it yourself. Something like http://procps.sourceforge.net. I've never used that, just the first thing I found in google.