demerphq has asked for the wisdom of the Perl Monks concerning the following question:
Hi Folks. I have a lot of different perl processes that run as scheduled jobs on a server. Im working under Win32 and so I often login via terminal services to check things out. One annoyance is that in the Win32 process list you see perl.exe and not the script name. And the tricks with setting $0 as far as I can tell dont work. But I know that the Win32 perl build is actualyl composed of two parts. The exe, which is about 20k and the dll which is much larger. The exe is in fact normally copied under two names perl.exe and perl5.6.1.exe for example. So I thought that if I used exec and a bit of cleverness I could simply make sure that for each of my scripts there was a custom named perl.exe for them to use. I came up with this:
package ex::NameExe; use strict; use warnings; use File::Spec::Functions qw(splitpath splitdir catdir catpath catfile +); use File::Copy; $|++; my (undef,undef,$script)=splitpath($0); $script or die $0; my ($v,$path,$exe)=splitpath($^X); $path=$v.$path; $script.=".exe"; my $new_exe=catfile($path,$script); unless ($exe eq $script) { copy $^X,$new_exe or die "$^X = > $new_exe:$!" unless -e $new_exe; exec $new_exe,'"-Mstrict;BEGIN{$|++};"',$0,@ARGV; die "Failed to re-execute as $new_exe!"; } 1;
The idea being that you can say perl -Mex::NameExe some_script.pl and magically what will happen is some_script.pl.exe will get created and the exec'ed like some_script.pl.exe some_script.pl but all behind the scenes. This appears to work, with the one problem that if I do it from the command line the first perl immediately exits and I dont see any of the output of the newly execed process. (This doesnt actually matter for most of the cron jobs, but a few of them are batch files and this behaviour of immediately returning will cause problems.)
Maybe im fundamentally misunderstanding things here, but this isnt the behaviour I was expecting, and the docs so far haven't clarified things. Any help would be appreciated.
I should say that what i thought would happen is that the new process would take over as though it had been run in the first place. If this is wrong then hopefully people will clarify. Cheers.
First they ignore you, then they laugh at you, then they fight you, then you win.
-- Gandhi
• Update:
Ive added in the $0 that PodMaster noticed was missing. So the problem wasnt to do with stdout/stderr at all. :-( But! I still dont understand the behaviour properly here. Why does it return immediately?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: exec()ed process dont write to std(?:err|out)
by PodMaster (Abbot) on Apr 13, 2004 at 11:17 UTC | |
by demerphq (Chancellor) on Apr 13, 2004 at 11:25 UTC | |
by PodMaster (Abbot) on Apr 13, 2004 at 11:44 UTC | |
|
Re: exec()ed process dont write to std(?:err|out) (system)
by tye (Sage) on Apr 13, 2004 at 14:57 UTC | |
by demerphq (Chancellor) on Apr 13, 2004 at 15:06 UTC | |
by tye (Sage) on Apr 13, 2004 at 15:30 UTC | |
by demerphq (Chancellor) on Apr 13, 2004 at 15:42 UTC | |
by tye (Sage) on Apr 13, 2004 at 16:00 UTC | |
|