Home
TeamSite
get iwsubmit return in perl ?
nipper
I have this command in a perl script:
$submit_cmd = "/u03/iw-home/bin/iwsubmit -x $file daily_update";
$out = `$submit_cmd`;
print LOG "command $submit_cmd \n out $out\n";
if ($out) {
finish ("File was NOT submitted \n");
} else {
print LOG "File submit successful \n";
}
This will run as a cron job, but I cannot catch an error. In debugging I submitted an error, & iwsubmit sent the error to STDERR, so $out was blank and my script went along.
How can I get the return code ?
TIA
ANdy
Find more posts tagged with
Comments
Migrateduser
I believe if you use
system
rather than backtics, it will return the command's return code and not the command's output.
Dave Smith
Sr. Software Engineer
Nike, Inc.
(503) 671-4238
DavidH.Smith@nike.com
nipper
Thanks, that spotted the error, I was hoping i could get the STDERR (like when I run iwodstart) but at least I see the error.
Andy
Johnny
$submit_cmd = "/u03/iw-home/bin/iwsubmit -x $file daily_update
2>&1
";
that last bit will redirect STDERR into STDOUT so you can catch all output.
$? will always return the last error code, so you could aslo use that if you were trying to get the output and the error code.
If you are using windows then the redirect wont work, but you should be fine.
John Cuiuli
Migrateduser
Sheesh, man - you really want it all, don't you?
Sure would be nice if all the CLTs behaved similarly as far as returning pass/fail info. ****, I'd be happy enough if they just documented the return codes for all the CLTs and what they meant. I ask for so much.
Dave Smith
Sr. Software Engineer
Nike, Inc.
(503) 671-4238
DavidH.Smith@nike.com
Johnny
dont let me start... some CLT use vpaths, some use file paths, atleast one uses vpaths with system back slashes!!.. In windows it can be a real pain.
John Cuiuli
Migrateduser
You can use open3 to get STDOUT/STDIN. Example:
use IPC:
pen3;
my $cmd="iwsubmit /foo ''";
my $pid = open3(*IN, *OUT, *ERR, $cmd);
close(IN); # give end of file to kid, or feed him
my
@error
= <ERR>;
my
@out
= <OUT>;
close(ERR);
close(OUT);
waitpid($pid,0);
my $returnCode=$?;
print
@error
;
print
@out
;
print $returnCode;
--
Jed Michnowicz
Interwoven Technical Consultant
james1
The problem with your open3() example is that if the program writes extensively to its stdout, you could hang. The OS (I think) will buffer the program's stdout, waiting for you to read it before allowing the program to write more to stdout. In the meantime, your Perl script is sitting there waiting for something to be written to stderr (or for stderr to be closed, which it won't be until the program is done writing everything else that it has for stdout).
Before doing something like this, you better be **** sure that the 2nd file handle you read won't fill its buffer.
The easiest way to get STDOUT and STDERR is to redirect both to files, and then read the two files. I suppose you could redirect only stderr, and use open2() (it does exist, doesn't it?).
-- James
--
James H Koh
Interwoven Engineering