SPUG: exec question

Tim Maher tim at consultix-inc.com
Fri Feb 11 09:57:44 PST 2005

On Fri, Feb 11, 2005 at 09:46:17AM -0800, Dan Ebert wrote:
> Here are some more details ... this is what I have:
> $script = '/home/dan/script.pl';
> $config = '/home/dan/config.cfg';
> $file_to_process = '/path/to/file';
> exec($script,$config,$file_to_process);

The problem is that exec blows away your current Perl process,
and runs $script in its place.   That's why your original
Perl process is never heard from again!
> (The $script expects 2 commandline args, the config file and the file to
> process.)
> I don't need to capture STDOUT or STDERR (the script executed has logging
> built in to it).
> The solution you suggest will probably work, I'm just curious why exec
> isn't returning immediately and the script exiting.  I thought that was
> what exec was supposed to do?

Nope!  Exec overwrites the memory image of the current process with
that of the named program.  You either want to use "system", which
uses the OS to start a new process for the script, or use "fork"
before the "exec", which will do what system does, but takes a bit
more coding and know-how to get right.

| Tim Maher, PhD     (206) 781-UNIX      (866) DOC-PERL     (866) DOC-UNIX |
| tim(AT)Consultix-Inc.Com  http://TeachMePerl.Com  http://TeachMeUnix.Com |
|      Watch for my upcoming book: "Minimal Perl for UNIX/Linux People"    |

More information about the spug-list mailing list