in reply to Trying to understand the System function
My first question is why you want to background this process when the first thing you are doing is checking for the existance of the output file. It is concievable that your process execution will not happen in time before Perl does its test for the file.
My next observation is that the reason your "ps" is failing is system thinks you are passing the ">", "test1.out" and "&" as arguments to "ps;" versus redirecting the output of "ps" to "test1.out".
is probably more in line with what you have in mind orsystem("/usr/bin/ps > test1.out");
if you are really intent on backgrounding the "ps" command.system("/usr/bin/ps > test1.out &");
Yet another way instead of using system would be as follows:
This way you background the process and wait for it before you do your test for file creation. YMMV based on how waitpid is implemented on your local system.my $pid=spawn("/usr/bin/ps","test1.out"); waitpid($pid,0); print "test1.out not created" if not -e "test1.out"; # # More code # sub spawn { my($cmd,$redirect)=@_; my $pid=fork(); return $pid if $pid > 0; if ( $pid == 0 ) { # Child process exec sprintf("%s > %s",$cmd,$redirect) or die "Could not exec $cmd:$!"; } else { # uh-oh... could not fork die "Could not fork $cmd : $!"; } }
There are lots of other ways of doing this, but this is my (current) favorite.
Peter @ Berghold . Net
Sieze the cow! Bite the day!
Test the code? We don't need to test no stinkin' code! All code posted here is as is where is unless otherwise stated.
Brewer of Belgian style Ales
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Trying to understand the System function
by sgifford (Prior) on Jul 28, 2003 at 21:00 UTC | |
by dakkar (Hermit) on Jul 29, 2003 at 09:05 UTC |