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.


---
demerphq

    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?



In reply to exec()ed process dont write to std(?:err|out) by demerphq

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.