How to run only 1 instance of compiled ROOT program?

I want to prevent users from starting a 2nd copy of a root program (root 5.24 compiled C++ program under linux).

Ideally, if a 2nd copy is started, it should just bring the running copy’s window to the front of the display, and then terminate. If it just informed the user and terminated, that would be fine.

Is there a good way to do this with some root functions?

(One way has been pointed out to me, but I’m hoping for something simpler. The suggestion is, at start up time, to have a thread open and listen on a socket. If the socket is already opened, it means a program copy is already running, so send a message to that socket, then terminate. If it receives a message on this socket, bring the program’s window to the front of the display.)

thanks,
buddy

Hi,

Here is a possible solution (for Linux only):

void test_root() { Ssiz_t idx; TString ps = gSystem->GetFromPipe("ps -C root.exe"); idx = ps.Index("root.exe", 0); if ((idx >= 0) && (ps.Index("root.exe", idx+8) >= 0)) { printf("Second instance of root.exe found! Exit!\n"); gApplication->Terminate(); } }
Cheers, Bertrand.

Thanks for your reply.

Is there a way I can find the executing program’s name at run time?
This would let me generalize the code. (at compile time, I maybe do not know what the program name will be at run time).

I see that GetFromPipe() is in 5.24, and we are on 5.22, but I could just pull the source snippet from 5.24.

buddy

My mistake on saying we were 5.24 in the original post.
Multiple systems, multiple versions.

Regarding different root versions -

If we have different versions communicating with TSockets and Tmonitor, should we use EnableSchemaEvolutionForAll(), as in the hclient tutorial?
And we also have windows and linux systems with different root versions.

thanks,
buddy

Hi buddy,

Sure:gSystem->BaseName(gApplication->Argv(0));
Cheers, Bertrand.

[quote]If we have different versions communicating with TSockets and Tmonitor, should we use EnableSchemaEvolutionForAll(), as in the hclient tutorial?
And we also have windows and linux systems with different root versions. [/quote]If you are not 100% guaranteed to have the same revision of ROOT on both end, yes you should enable the schema evolution support.

Cheers,
Philippe.

And for the record, here is a cross-platform version of the script:

void test_root() { // script testing if another instance of the program is already running Ssiz_t idx; TString appName = gSystem->BaseName(gApplication->Argv(0)); TString ps; TString os = gSystem->GetBuildArch(); if (os.Contains("win32")) { if (!appName.EndsWith(".exe")) appName += ".exe"; ps = gSystem->GetFromPipe(TString::Format("tasklist /FI \"IMAGENAME eq %s\"", appName.Data())); } else if (os.Contains("macosx")) ps = gSystem->GetFromPipe(TString::Format("ps axco command | grep %s", appName.Data())); else if (os.Contains("linux")) ps = gSystem->GetFromPipe(TString::Format("ps -C %s", appName.Data())); else { printf("Unknown Architecture! (%s)\n", os.Data()); return; } idx = ps.Index(appName.Data(), 0); if ((idx >= 0) && (ps.Index(appName.Data(), idx+appName.Length()) >= 0)) { printf("Second instance of %s found! Exit!\n", appName.Data()); gApplication->Terminate(); } }
Cheers, Bertrand.