Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capturing STDOUT, STDIN and Return Code from external command 2

Status
Not open for further replies.

JJ1

Programmer
Apr 19, 2001
104
GB
Folks,

I'm fairly new to Perl and UNIX, but have been placed with the UNIX team at work for a month.

My first task is to create a script which mounts NFS shares and raises an alert upon failure (we have a system called 'Remedy' which displays incident alerts to a 24-7 team).

To generate a Remedy alert, we have a UNIX binary, which returns zero on success and another number on failure. It also outputs to STDOUT or STDERR on failure (at least that's now I understand it, but am unsure how to test this).

What I really need is to capture the command's return code, but also keep a record of the STDOUT/STDERR (for logging purposes).

system() gives me the return code, but no STDOUT/STDERR whilst backticks (` `) do the opposite.

The command I'm running is:
[red]
system( "/bin/su", "-c", "/home/remedy/Remedy_gentkt -s \"$fault_title\" -L \"$description\"", "non_root_user" );
[/red]
I'm sure there must be a way. Any suggestions would be greatfully received ;-)
 
The only thing i can think of is you could direct STDOUT&STDERR to a file which is done like this at your system call:

2>&1 1>somefile.txt

Code:
system( "/bin/su", "-c", "/home/remedy/Remedy_gentkt -s \"$fault_title\" -L \"$description\"", "non_root_user" 2>&1 1>somefile.txt);

I THINK, I also suck at unix so you might want to google 'outputing STDOUT' or something like that :)!

andy
 
2 represents STDERR and 1 represents STDOUT by the way, so were are making STDERR go to STDOUT and then wacking that into the file :)
 
Code:
system(my_huge_unix_command | tee output.out);
This will print the STDOUT/STDERR on the screen (if there is any) and at the same time will print them inside the file output.out


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Andyros,

That's a great suggestion. Based on your answer, I think this is what I need:

[Red]
system( "/bin/su", "-c", "/home/remedy/Remedy_gentkt -s \"$fault_title\" -L \"$description\"", "non_root_user" 1>/tmp/stdout 2>/tmp/stderr );
[/Red]

It's slightly messy because I then have to open the file (using open() ?) and delete it when finished.

I'll go with this unless someone can think of anything better.

Thanks,

James.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top