Sprad has asked for the wisdom of the Perl Monks concerning the following question:

I'm having a hard time getting this to work as expected:
Win32::Process::Create(my $ProcessObj, "c:\\windows\\system32\\cmd.exe", "/c c:\\test\\slowproc.cmd | tee.exe processlog";, 0, NORMAL_PRIORITY_CLASS, ".") || die ErrorReport(); unless ($ProcessObj->Wait($timeout * 1000)) { $ProcessObj->Kill(1) };
When I run it, the Kill works and returns control to the Perl script, but the slowproc.cmd and tee keep on going in the background. If I just run slowproc without tee, Kill ends it properly.

The PID in question is the PID of the cmd.exe process, as you'd expect. Doing tests on the command line show that it works if you put quotes around the "slowproc.cmd | tee processlog" part, but that doesn't seem to have any effect in the Perl script.

Has anybody gotten chained commands like this to work with Win32::Process?

---
A fair fight is a sign of poor planning.

Replies are listed 'Best First'.
Re: Win32::Process and tee
by BrowserUk (Patriarch) on Mar 23, 2004 at 05:06 UTC

    If your doing this on 2k/2k3/XP then I strongly suggest that you take a look at Win32::Job. It uses the recently added "Job objects" api's to make doing this kind of thing trivial rather than nearly impossible as previously.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
      I took a look at Win32::Job, and it looks like that'll work great for what I'm trying to do. Thanks!

      ---
      A fair fight is a sign of poor planning.

Re: Win32::Process and tee
by paulbort (Hermit) on Mar 23, 2004 at 03:41 UTC
    The quotes are important because of what they do: Without them, the shell catches the pipe before the shell you're invoking (cmd.exe) even sees them.
    Without quotes:
    { C:\Windows\System32\cmd.exe /c c:\test\slowproc.cmd } | tee.exe proc +esslog
    with quotes (hopefully):
    C:\Windows\System32\cmd.exe /c { " c:\test\slowproc.cmd | tee.exe proc +esslog " }
    If you could get rid of the call to tee.exe, you could get rid of using cmd.exe too. Much as I dislike it when a monk rashly suggests the OP turn left after requesting help with turning right, here goes:

    Instead of the cmd.exe/tee.exe adventure, have you considered using Perl's fork(), then in the child process: open CMD, "slowproc.cmd|";? You could then write anything you get on <CMD> to STDOUT and a log file. (OK, my Camel is at work, so I'm hoping that I did that right from memory. I will trust the other monks to gently correct if not.)

    If you're not interested in going down the fork()'ed path, the other option would be to create a wrapper.cmd that invokes slowproc.cmd with the pipe. Then ask cmd.exe to run wrappers.cmd.

    Hope this helps.

    --
    Spring: Forces, Coiled Again!