How to save the output of the result of a shell command

Dear rooters,

I am writing a macro for a ROOT/CINT based program that convert raw data to TTree.
In this macro I would like to test if my raw data file is not open somewhere else (on the network typically), especially by the acquisition system (data being wrote).

Is there any root method to do that ?

I found on UNIX the command “lsof”, which does exactly what I want.
http://www.netadmintools.com/html/lsof.man.html
So an alternative will be to execute this shell command

gSystem->Exec("lsof")

and parse the output of lsof to check if the file of interest is not present. But TSystem::Exec only return an integer. Is there any way to do this ? Like you can do it in Perl for example…

Thank you in advance for your help.
Best
Julien

Using root 5.23/04 or newer you can do

TString s = gSystem->GetOpenPipe(system command); eg

TString s = gSystem->GetOpenPipe("lsof");
Now up to you to get the info in the TString s, eg

Rene

Dear Rene,

Thank you for pointing me this method.
I will thus update my version of ROOT

Best
Julien

Hello,
with older versions of root (< 5.23.04) you can do:

 TString cmd ("tail -20 marabou.log");
 TString line;
 FILE *fp = gSystem->OpenPipe(cmd, "r");
 line.Gets(fp);
 gSystem->ClosePipe(fp);

Cheers
Otto

Dear Otto,

Thank for this solution. I prefer this since updating ROOT in my lab is not an easy task (updating is global…).

Concerning the very particular problem I was facing at the beginning of my post, I found a workaround that does not imply streaming the output of the shell command.

I wanted to check if a given file was opened so here is the code I used :

string input_file = "name_of_the_file_to_be_tested";
string condition = "lsof -Fn  | grep ";
condition += input_file;
if(gSystem->Exec(condition.c_str())!=0) {
// code if the file is not opened
}

Hope this can help others.
Best
Julien

Dear Rene,

I realize this is an old thread but did you mean

“TString s = gSystem->GetFromPipe(system command)”

above? There seems to be no “gSystem->GetOpenPipe(…)” in:

http://root.cern.ch/root/html/TSystem.html

Perhaps I overlooked something.

Thanks,
Anthony

Yes, sorry from the typo, I meant:

TString s = gSystem->GetFromPipe(system command)
Rene