Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^4: Perl Background processes in Windows

by gepebril69 (Scribe)
on Jan 31, 2012 at 10:22 UTC ( [id://950929]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Perl Background processes in Windows
in thread Perl Background processes in Windows

@Peter Dragon, You are right. Maybe I posted to quickly, but no matter what $cmd I use (dir|date|xxxx.exe) it will always fail on my Windows7 x64 machine. It looks very strange to me that you can use a system call in that way. Normally you use something like `$cmd` or system() to execute a system call.

Replies are listed 'Best First'.
Re^5: Perl Background processes in Windows
by Corion (Patriarch) on Jan 31, 2012 at 10:26 UTC

    Consider looking at perlop to see what -x does. Also, if it "always fails" (and you're interested in learning why), consider posting full code.

    Please note that dir and date are shell builtins in cmd.exe and thus never pass -x, because they are not programs that can be run. Also note that -x expects the full path to an executable and does not search $ENV{PATH}. Whether that is desired or not in the context of the above program, I don't know. Update: As the argument is passed to Win32::Process::Create(...), it makes sense to have a fully specified path, because that's what Win32::Process::Create() likely wants.

    >perl -wle "print $_, -x $_ ? ' yes':' no' for @ARGV" cmd.exe c:\WINDO +WS\system32\cmd.exe cmd.exe no c:\WINDOWS\system32\cmd.exe yes

    As to using system or backticks, these do not launch a background process. Which seems to be the purpose of the code under discussion. Actually, perlport points out system(1, ...) for launching a process in the background under Windows and OS/2.

      Proc::Background - Generic interface to Unix and Win32 background

      use Proc::Background; timeout_system($seconds, $command, $arg1); timeout_system($seconds, "$command $arg1"); my $proc1 = Proc::Background->new($command, $arg1, $arg2); my $proc2 = Proc::Background->new("$command $arg1 1>&2"); $proc1->alive; $proc1->die; $proc1->wait; my $time1 = $proc1->start_time; my $time2 = $proc1->end_time; # Add an option to kill the process with die when the variable + is # DETROYed. my $opts = {'die_upon_destroy' => 1}; my $proc3 = Proc::Background->new($opts, $command, $arg1, $arg +2); $proc3 = undef;
        Thanks very much for your help and information. With this info I could solve my issues rapidly. I rebuild the example of Peter Dragon for my usage:
        sub start_child { my $Cmd = $_[0]; my $CmdOptions = $_[1]; my $ChildProc; my $ChildPid; die "cannot execute cmd: $Cmd" unless (-x "$Cmd"); require Win32::Process; Win32::Process::Create($ChildProc, $Cmd, $CmdOptions, 0, 0, ".") || +confess "Could not spawn child: $!"; $ChildPid = $ChildProc->GetProcessID(); # catch early child exit, e.g. if program path is incorrect sleep(1.0); POSIX::waitpid(-1, POSIX::WNOHANG()); # clean up any defunct child p +rocess if (kill(0,$ChildPid)) { print "Started child process id $ChildPid\n"; } else { warn "Child process exited quickly: $Cmd: process $ChildPid"; } return $ChildPid; } $ChildPid = start_child($Cmd1, $Cmd1Options); sleep(5.0); $ChildPid = start_child($Cmd2, $Cmd2Options);
        At the moment I stop the childs by TASKKILL /F /PID of the commands I've started. Not the nicest solution, better to do it with interrupts, but I haven't read that part of the Perl cookbook yet ;)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://950929]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2024-03-29 08:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found