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

I am trying to initiate the execution of monitor.pl by using this following pipe mechanism: $cpid = open($fh, '-|', "./monitor.pl >/dev/null") or die "can not open pipe\n"; The output of monitor.pl is redirected to /dev/null. The problem which I am facing is that I am not able to kill the processes even after using the following code: kill ('INT', $cpid) if defined $cpid; close $fh if defined $fh; So please can anyone suggest me how to kill the process monitor.pl >/dev/null.
  • Comment on How to kill a process redirected to /dev/null

Replies are listed 'Best First'.
Re: How to kill a process redirected to /dev/null
by BrowserUk (Patriarch) on Jul 19, 2013 at 07:50 UTC

    Why are you piping the output into your program if you are redirecting it to /dev/null?


    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.
Re: How to kill a process redirected to /dev/null
by mtmcc (Hermit) on Jul 19, 2013 at 07:37 UTC
    Does your process show up on the list made by this script?:

    #! /usr/bin/perl use strict; use warnings; my $list = `ps -a`; print STDERR "$list";

Re: How to kill a process redirected to /dev/null
by mtmcc (Hermit) on Jul 20, 2013 at 08:22 UTC
    I agree with browser that it's not clear what you want to do.

    If you wan't to kill monitor.pl (if it's running) from a separate script, this should work:

    #! /usr/bin/perl use strict; use warnings; my $list = `ps -a`; print STDERR "$list"; my @processLine; my @processesArray = split("\n", $list); print STDERR "$processesArray[0]\n"; for (@processesArray) { if ($_ =~ m/monitor.pl/) { @processLine = split(" ", $_); system ("kill $processLine[0]"); print STDERR "$processLine[$#processLine] killed.\n"; } }

    On *nix systems at least, because the OS processes the command line before running the original script (ie. "monitor.pl > /dev/null"), I don't know of a way to distinguish instances of a script based on their redirection targets.

    I hope that helps.