Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Terminating a parallel process opened with "system(..)"

by palkia (Scribe)
on May 29, 2012 at 22:51 UTC ( #973140=perlquestion: print w/ replies, xml ) Need Help??
palkia has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone.
I'm looking for a way to terminate a (parallel) process opened with by
system('start','','some text file for example.txt');
preferably without relaying on non-core modules.

Your thoughts ?
Thx

Comment on Terminating a parallel process opened with "system(..)"
Download Code
Re: Terminating a parallel process opened with "system(..)"
by Anonymous Monk on May 29, 2012 at 23:05 UTC
Re: Terminating a parallel process opened with "system(..)"
by ikegami (Pope) on May 29, 2012 at 23:43 UTC
    start is a shell command, so
    system('start "" "some text file for example.txt"');
Re: Terminating a parallel process opened with "system(..)"
by saeen (Novice) on May 30, 2012 at 00:24 UTC
    I needed to something similar. AS the other post mentions Proc::Background did the trick. Here is the sample code. I wanted to listen on specific ports and then run an nmap scan from external host to see if the ports are reachable. This was part of a larger tool.
    #!/bin/perl -w use strict; use warnings; use Proc::Background; use Net::OpenSSH; my @ports = ("443","8080"); my @threads = ''; my $index = 0; foreach my $port(@ports){ $threads[$index] = Proc::Background->new("/usr/bin/perl thread +.pl $port"); $index++; } my $ssh = Net::OpenSSH->new("remote.host.net", user=>"me",passwd=> +"******"); $ssh->error and die "Can't ssh to host\n"; my @output = $ssh->capture("nmap -p T:8080,443 my.host.name"); print @output; foreach my $thread (@threads){ print "Killing $thread\n"; $thread->die; } print "Done ";
Re: Terminating a parallel process opened with "system(..)"
by BrowserUk (Pope) on May 30, 2012 at 00:41 UTC
    I'm looking for a way to terminate a (parallel) process opened with by system('start','','some text file for example.txt'); preferably without relaying on non-core modules.

    There is no way to do that using start.

    The start command is a cmd.exe built-in, so system has to start a new copy of cmd.exe first, to run the start command. Then the start command determines the path & name of the executable associated with the filename you supplied and runs it. There is no way for Perl to know the process id of the process that the start command runs.

    The only way to achieve your goal would be to emulate what the start command does -- look up the ftype assoc'd to the file type; then look up the path/executable name for that ftype; then invoke that executable directly.

    When run as asyncStart-kill test.png, this starts my image editor with the test.png as its argument; waits 10 seconds and then kills it:

    #! perl -slw use strict; my $filename = $ARGV[0]; my( $ext ) = $filename =~ m[(\.[^.]+)$]; my( $assoc ) = `assoc $ext` =~ m[=(.+)$]; my( $ftype ) = `ftype $assoc` =~ m[=(.+)$]; $ftype =~ s[^("[^"]+?"|\S+)\K.+$][]; #" print $ftype; my $pid = system 1, $ftype, $filename; print $pid; sleep 10; kill 9, $pid;

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Thank you for your replay.
      I tired your code but the process isn't being "killed",
      The exact same thing is happening with and without the "kill" line.
      not when it's a txt in notepad,
      nor a pl file in the terminal,
      nor an exe file converted from a pl file.
      In the last 2, it didn't even open the process in a new window, but rather in the same terminal (weird).

      Your thoughts ?
      Thx

        See how you get on with this version?

        #! perl -slw use strict; my $filename = $ARGV[0]; my( $ext ) = $filename =~ m[(\.[^.]+)$]; my( $assoc ) = `assoc $ext` =~ m[=(.+)$]; my( $ftype ) = `ftype $assoc` =~ m[=(.+)$]; $ftype =~ s[%(\w+)%][ $ENV{ $1 } ]ge; $ftype =~ s[^("[^"]+?"|\S+)\K.+$][]; #" print $ftype, ':', $filename; my $pid = system 1, $ftype, $filename; print $pid; sleep 10; kill 9, $pid;

        Note: This isn't intended to be a complete replacement for the start command, just a starting point for you to adapt to your needs.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://973140]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2014-08-27 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (253 votes), past polls