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 or if you are really intent on backgrounding the "ps" command.

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.

There are lots of other ways of doing this, but this is my (current) favorite.

References:


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
    What's the advantage of the spawn function over just using system?

      Done in that way, there is no difference: exec will start a shell, just as system does.

      What the OP wants is:

      • fork, then in the child process:
      • open the output file and attach it to STDOUT
      • exec with the list of parameters (it will not invoke a shell)
      • meanwhile, in the parent process, you can wait for the child to finish

      For more info, you can look at "perlipc" and the "fork" entry in "perlfunc"

      -- 
              dakkar - Mobilis in mobile
      

      Most of my code is tested...

      Perl is strongly typed, it just has very few types (Dan)