kill command in a perl script

Discussion in 'Perl Misc' started by Hugh Kang, Sep 15, 2003.

  1. Hugh Kang

    Hugh Kang Guest

    I just started learning Perl and I am trying to do followings;

    ps -ef | grep java >pidfile
    In pidfile, there are 3 PIDs for weblogic processes.

    root 1769 1758 TS 0 0 17:55:26 vt02 44:38
    /opt/java2-1.3.1/bin/./../bin/x86at/native_threads/java -Xms512m
    -Xmx512m -Dweb
    root 27464 27453 TS 29 0 09:15:55 vt04 91:55
    /opt/java2-1.3.1/bin/./../bin/x86at/native_threads/java -Xms1024m
    -Xmx1024m -Dw
    root 27533 27522 TS 49 0 09:21:45 vt03 2:35
    /opt/java2-1.3.1/bin/./../bin/x86at/native_threads/java -Xms32m
    -Xmx200m -Dwebl

    What I want is get the largest PID which is 27533 in this case, and
    then
    kill -3 the PID.

    So I made a simple one for this:

    #!/usr/bin/perl
    #
    # This is a test script to get wls managed server pid
    #
    #
    open(JAVAPIDS,"pidfile") or die "can't open input file:$!\n";
    $pidcnt=0;
    while($line=<JAVAPIDS>) {
    # ($user,$pid1,$pid2,$it4,$it5,$it6,$it7,$it8,$it9,$it10,$it11,$it12,$it13,$it14,$it15)=split("
    ",$line);
    ($user,$pid1)=split(" ",$line);
    $pidfile{$pid1}=$pid1;
    $pid_comp[$pidcnt]=$pidfile{$pid1};
    # print "pid$pidcnt : $pid_comp[$pidcnt] \n";
    if ($pidcnt gt 0) {
    if ($pid_comp[0] < $pid_comp[$pidcnt]) {
    $pid_comp[0] = $pid_comp[$pidcnt];
    }
    }
    $pidcnt++;

    }
    print "pidcnt : $pidcnt \n";
    print "Largest PID for java is : $pid_comp[0] \n";

    ------------------------------------------

    Q1) How do I do 'kill -3 $pid_comp[0] in this script?
    Q2) Is there any way that I can do the followings:

    In a Unix script,

    ps -ef |grep java >pidfile

    ../perl_script
    ....
    ....
    kill -3 $pid_comp[0]


    Q3) Any other way to get what I want?


    Can anyone help me out with this issue please?

    Many thanks in advance!

    Hugh
     
    Hugh Kang, Sep 15, 2003
    #1
    1. Advertising

  2. Hugh Kang

    Ted Zlatanov Guest

    On 15 Sep 2003, wrote:

    > I just started learning Perl and I am trying to do followings;
    >
    > ps -ef | grep java >pidfile
    > In pidfile, there are 3 PIDs for weblogic processes.
    >
    > root 1769 1758 TS 0 0 17:55:26 vt02 44:38
    > /opt/java2-1.3.1/bin/./../bin/x86at/native_threads/java -Xms512m
    > -Xmx512m -Dweb
    > root 27464 27453 TS 29 0 09:15:55 vt04 91:55
    > /opt/java2-1.3.1/bin/./../bin/x86at/native_threads/java -Xms1024m
    > -Xmx1024m -Dw
    > root 27533 27522 TS 49 0 09:21:45 vt03 2:35
    > /opt/java2-1.3.1/bin/./../bin/x86at/native_threads/java -Xms32m
    > -Xmx200m -Dwebl
    >
    > What I want is get the largest PID which is 27533 in this case, and
    > then kill -3 the PID.


    Use Proc:processTable from CPAN.

    <offtopic>
    I should warn you that the "largest" PID is meaningless - new
    processes do not necessarily have larger PIDs than older processes on
    any OS I have used, including Linux. You should find a better way to
    discover the process you want to kill. You could use the daemontools
    package, for instance.
    </offtopic>

    Ted
     
    Ted Zlatanov, Sep 15, 2003
    #2
    1. Advertising

  3. Hugh Kang

    Hugh Kang Guest

    Jim Gibson <> wrote in message news:<150920031752553144%>...
    > In article <>, Hugh Kang
    > <> wrote:
    >
    > > I just started learning Perl and I am trying to do followings;
    > >
    > > ps -ef | grep java >pidfile
    > > In pidfile, there are 3 PIDs for weblogic processes.
    > >
    > > root 1769 1758 TS 0 0 17:55:26 vt02 44:38
    > > /opt/java2-1.3.1/bin/./../bin/x86at/native_threads/java -Xms512m
    > > -Xmx512m -Dweb
    > > root 27464 27453 TS 29 0 09:15:55 vt04 91:55
    > > /opt/java2-1.3.1/bin/./../bin/x86at/native_threads/java -Xms1024m
    > > -Xmx1024m -Dw
    > > root 27533 27522 TS 49 0 09:21:45 vt03 2:35
    > > /opt/java2-1.3.1/bin/./../bin/x86at/native_threads/java -Xms32m
    > > -Xmx200m -Dwebl
    > >
    > > What I want is get the largest PID which is 27533 in this case, and
    > > then
    > > kill -3 the PID.
    > >
    > > So I made a simple one for this:
    > >
    > > #!/usr/bin/perl
    > > #
    > > # This is a test script to get wls managed server pid
    > > #
    > > #
    > > open(JAVAPIDS,"pidfile") or die "can't open input file:$!\n";
    > > $pidcnt=0;
    > > while($line=<JAVAPIDS>) {
    > > #
    > > ($user,$pid1,$pid2,$it4,$it5,$it6,$it7,$it8,$it9,$it10,$it11,$it12,$it13,$it14,
    > > $it15)=split("
    > > ",$line);

    >
    > The above line is unnecessary because of the following line.
    >
    > > ($user,$pid1)=split(" ",$line);
    > > $pidfile{$pid1}=$pid1;
    > > $pid_comp[$pidcnt]=$pidfile{$pid1};
    > > # print "pid$pidcnt : $pid_comp[$pidcnt] \n";
    > > if ($pidcnt gt 0) {
    > > if ($pid_comp[0] < $pid_comp[$pidcnt]) {
    > > $pid_comp[0] = $pid_comp[$pidcnt];
    > > }
    > > }
    > > $pidcnt++;
    > >
    > > }
    > > print "pidcnt : $pidcnt \n";
    > > print "Largest PID for java is : $pid_comp[0] \n";
    > >
    > > ------------------------------------------
    > >
    > > Q1) How do I do 'kill -3 $pid_comp[0] in this script?

    >
    > kill 3, $pid_comp[0]
    >
    > See 'perldoc -f kill'
    >
    > > Q2) Is there any way that I can do the followings:
    > >
    > > In a Unix script,
    > >
    > > ps -ef |grep java >pidfile
    > >
    > > ./perl_script
    > > ...
    > > ...
    > > kill -3 $pid_comp[0]

    >
    > Not easily, and not necessary because of answer to 1.
    >
    > >
    > >
    > > Q3) Any other way to get what I want?

    >
    > Yes. You can get the filtered output of the ps command using backtics:
    >
    > [tested in part ("No processes were actually killed in the development
    > of this program")]
    >
    > #!/opt/perl/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > my @lines = `ps -ef|grep java`; # <--notice backtics, not single quotes
    > my @pids = ();
    > foreach ( @lines) {
    > my ($user,$pid) = split;
    > push(@pids,$pid);
    > }
    > my $largest = ((sort(@pids)))[$#pids];
    > kill 3, $largest;
    >
    > [end program]
    >
    > >
    > >
    > > Can anyone help me out with this issue please?

    >
    > Hope I have.
    >
    > >
    > > Many thanks in advance!
    > >
    > > Hugh




    Thanks a lot. I have three more things to ask please.

    q1) When I do my @lines = `ps -ef | grep java`;, pid for "grep java" from ps -ef
    is included in @lines. I'd like to exclude this one. How do I do that?
    When I do, ps -ef | grep java > pidfile on Unix shell, I don't see the pid
    for 'grep java' in pidfile.

    q2) Later I'd like to check the 11th column which can be "-Xms1024m" and issue
    kill -3 $pid if it is "-Xms1024m". How do I check it using the above script?

    q3) kill -3 $pid is not killing the process. It makes a thread dump for weblogic
    server. In Unix script, I check an error from log file and want to take a
    thread dump using the perl script when the error occurs. How can I run the perl
    script in Unix script? Or any other way to do this?


    I just ordered Perl manuals to study and practice more.

    Thanks again.

    Hugh
     
    Hugh Kang, Sep 16, 2003
    #3
  4. Hugh Kang wrote:
    >
    > q1) When I do my @lines = `ps -ef | grep java`;, pid for "grep java" from ps -ef
    > is included in @lines. I'd like to exclude this one. How do I do that?
    > When I do, ps -ef | grep java > pidfile on Unix shell, I don't see the pid
    > for 'grep java' in pidfile.


    my @lines = grep /java/, `ps -ef`;

    Or if you just want the PIDs and have the appropriate version of ps or
    pgrep:

    my @pids = `ps h -C java -o pid`;

    my @pids = `pgrep java`;


    > q2) Later I'd like to check the 11th column which can be "-Xms1024m" and issue
    > kill -3 $pid if it is "-Xms1024m". How do I check it using the above script?


    My version of "ps -ef" doesn't have eleven columns

    kill 'QUIT', grep { /-Xms1024m/ and $_ = (split)[0] } `ps h -C java -o
    pid,command`;


    > q3) kill -3 $pid is not killing the process.


    You do realise that using a negative number with kill is different then
    using a positive number.

    perldoc -f kill
    [snip]
    Unlike in the shell, if SIGNAL is negative, it
    kills process groups instead of processes. (On
    System V, a negative PROCESS number will also kill
    process groups, but that's not portable.) That
    means you usually want to use positive not
    negative signals. You may also use a signal name
    in quotes. See the Signals entry in the perlipc
    manpage for details.



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Sep 16, 2003
    #4
  5. Hugh Kang

    Ted Zlatanov Guest

    [lots of awkward ps -ef gymnastics omitted]

    Good god, guys, why are you stubbornly trying to reinvent the wheel?
    I mean, it's fun to do it sometimes, but Hugh (a new Perl programmer)
    should absolutely not be trying to manually parse the output of
    ps -ef, creating a nonportable script with weird bugs (what happens
    if "grep" is in the process name you want, for instance? what happens
    on systems where ps -ef does not do what you think it does?)

    Please use Proc::processTable from CPAN. It will save you time and
    lots of headaches.

    Ted
     
    Ted Zlatanov, Sep 17, 2003
    #5
  6. Ted Zlatanov wrote:
    >
    > [lots of awkward ps -ef gymnastics omitted]
    >
    > Good god, guys, why are you stubbornly trying to reinvent the wheel?
    > I mean, it's fun to do it sometimes, but Hugh (a new Perl programmer)
    > should absolutely not be trying to manually parse the output of
    > ps -ef, creating a nonportable script with weird bugs (what happens
    > if "grep" is in the process name you want, for instance? what happens
    > on systems where ps -ef does not do what you think it does?)
    >
    > Please use Proc::processTable from CPAN. It will save you time and
    > lots of headaches.


    $ cat Proc-ProcessTable-0.38/README
    Proc::processTable, version .38

    STATUS
    ======
    This is BETA software; it seems to work, but use at your own risk :)
    [snip]


    If that's not enough of a reason the module requires a C compiler to
    install and all the C code does (on FreeBSD, IRIX, DecOSF, NetBSD,
    Linux, UnixWare and Solaris) is access the /proc file system


    A code example from ProcessTable.pm

    sub _get_tty_list
    {
    my ($self) = @_;
    undef %Proc::processTable::TTYDEVS;
    find({ wanted =>
    sub{
    $File::Find::prune = 1 if -d $_ && ! -x $_;
    my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
    $atime,$mtime,$ctime,$blksize,$blocks) =
    stat($File::Find::name);
    $Proc::processTable::TTYDEVS{$rdev} = $File::Find::name
    if(-c $File::Find::name);
    }, no_chdir => 1},
    "/dev"
    );
    }


    Why are they stat()ing the same file FOUR times?


    Not that I'm against using modules. :) I just like to know what all
    my options are.


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Sep 17, 2003
    #6
  7. [This followup was posted to comp.lang.perl.misc]

    In article <>,
    says...
    > I just started learning Perl and I am trying to do followings;
    >
    > ps -ef | grep java >pidfile
    > In pidfile, there are 3 PIDs for weblogic processes.
    >
    > root 1769 1758 TS 0 0 17:55:26 vt02 44:38
    > /opt/java2-1.3.1/bin/./../bin/x86at/native_threads/java -Xms512m
    > -Xmx512m -Dweb
    > root 27464 27453 TS 29 0 09:15:55 vt04 91:55
    > /opt/java2-1.3.1/bin/./../bin/x86at/native_threads/java -Xms1024m
    > -Xmx1024m -Dw
    > root 27533 27522 TS 49 0 09:21:45 vt03 2:35
    > /opt/java2-1.3.1/bin/./../bin/x86at/native_threads/java -Xms32m
    > -Xmx200m -Dwebl
    >
    > What I want is get the largest PID which is 27533 in this case, and
    > then
    > kill -3 the PID.
    >
    > So I made a simple one for this:
    >
    > #!/usr/bin/perl
    > #
    > # This is a test script to get wls managed server pid
    > #
    > #
    > open(JAVAPIDS,"pidfile") or die "can't open input file:$!\n";
    > $pidcnt=0;
    > while($line=<JAVAPIDS>) {
    > # ($user,$pid1,$pid2,$it4,$it5,$it6,$it7,$it8,$it9,$it10,$it11,$it12,$it13,$it14,$it15)=split("
    > ",$line);
    > ($user,$pid1)=split(" ",$line);
    > $pidfile{$pid1}=$pid1;
    > $pid_comp[$pidcnt]=$pidfile{$pid1};
    > # print "pid$pidcnt : $pid_comp[$pidcnt] \n";
    > if ($pidcnt gt 0) {
    > if ($pid_comp[0] < $pid_comp[$pidcnt]) {
    > $pid_comp[0] = $pid_comp[$pidcnt];
    > }
    > }
    > $pidcnt++;
    >
    > }
    > print "pidcnt : $pidcnt \n";
    > print "Largest PID for java is : $pid_comp[0] \n";
    >
    > ------------------------------------------
    >
    > Q1) How do I do 'kill -3 $pid_comp[0] in this script?
    > Q2) Is there any way that I can do the followings:



    Perl has a "kill" function. For more details do the following :

    perldoc -f kill
     
    Barry Kimelman, Sep 17, 2003
    #7
  8. "John W. Krahn" <> wrote in message news:<>...
    > Ted Zlatanov wrote:
    > >
    > > [lots of awkward ps -ef gymnastics omitted]
    > >
    > > Good god, guys, why are you stubbornly trying to reinvent the wheel?
    > > I mean, it's fun to do it sometimes, but Hugh (a new Perl programmer)
    > > should absolutely not be trying to manually parse the output of
    > > ps -ef, creating a nonportable script with weird bugs (what happens
    > > if "grep" is in the process name you want, for instance? what happens
    > > on systems where ps -ef does not do what you think it does?)
    > >
    > > Please use Proc::processTable from CPAN. It will save you time and
    > > lots of headaches.

    >
    > $ cat Proc-ProcessTable-0.38/README
    > Proc::processTable, version .38
    >
    > STATUS
    > ======
    > This is BETA software; it seems to work, but use at your own risk :)
    > [snip]


    Dan should really remove that. His code has been around long enough,
    and is stable enough, that I consider it production quallity. Having
    ported some of his code to Ruby, I can tell you that it's trustworthy,
    i.e. passes all tests, no memory leaks, and does what it's supposed to
    do.

    > If that's not enough of a reason the module requires a C compiler to
    > install and all the C code does (on FreeBSD, IRIX, DecOSF, NetBSD,
    > Linux, UnixWare and Solaris) is access the /proc file system


    It requires a C compiler - so what? If you're on *BSD or Linux,
    you've almost certainly have gcc anyway. Solaris users will likely
    have some compiler (gcc or Sun's Forte compiler). I can't speak for
    UnixWare, IRIX or DecOSF.

    In addition, reading out of /proc for Linux and *BSD is simple enough
    with pure Perl, but it will still be faster with C. Parsing /proc
    with pure Perl would be quite a challenge on Solaris, considering that
    the information in /proc is NOT in plaintext format.

    > A code example from ProcessTable.pm
    >
    > sub _get_tty_list
    > {
    > my ($self) = @_;
    > undef %Proc::processTable::TTYDEVS;
    > find({ wanted =>
    > sub{
    > $File::Find::prune = 1 if -d $_ && ! -x $_;
    > my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
    > $atime,$mtime,$ctime,$blksize,$blocks) =
    > stat($File::Find::name);
    > $Proc::processTable::TTYDEVS{$rdev} = $File::Find::name
    > if(-c $File::Find::name);
    > }, no_chdir => 1},
    > "/dev"
    > );
    > }
    >
    >
    > Why are they stat()ing the same file FOUR times?


    Hmm....I don't remember this code, so I can't say much about it. The
    author, Dan Urist, would probably be willing to explain this code if
    you ask him.

    > Not that I'm against using modules. :) I just like to know what all
    > my options are.
    >
    >
    > John


    Your choices are to parse 'ps' (blech) or use Proc::processTable. As
    others have noted, parsing ps is utterly unreliable and not portable
    because the options are different on different platforms, and
    sometimes they're different on the *same* platform between releases.
    You can always submit pure Perl patches to Dan for Linux and the *BSD
    family if the C compiler thing really bothers you that much. Wouldn't
    be that difficult.

    If there's any "flaw" to Dan's code it's that he added the formatting
    cruft, presumably because ps gives you output formatting options.
    I'll bet if he had to do it over again, he would rip that out, but
    that's just a guess. I can tell you that *I* will never add it. :)

    Regards,

    Dan (Berger)
     
    Daniel Berger, Sep 17, 2003
    #8
  9. Hugh Kang

    Bart Lateur Guest

    Ted Zlatanov wrote:

    >I should warn you that the "largest" PID is meaningless - new
    >processes do not necessarily have larger PIDs than older processes on
    >any OS I have used, including Linux.


    Indeed. The reason is simple: wraparound beyond a fixed number of bits
    for the ID numbers. So after a while they start back at zero. (Or 1...?)

    --
    Bart.
     
    Bart Lateur, Sep 18, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,830
  2. Jean-Paul Calderone
    Replies:
    3
    Views:
    1,708
    Thomas Bellman
    May 10, 2008
  3. Diez B. Roggisch
    Replies:
    0
    Views:
    631
    Diez B. Roggisch
    May 8, 2008
  4. Replies:
    2
    Views:
    165
    Tad McClellan
    Mar 26, 2005
  5. Swarup Baran

    Handling KILL signal in a perl script

    Swarup Baran, Jul 25, 2006, in forum: Perl Misc
    Replies:
    12
    Views:
    742
    Josef Moellers
    Jul 26, 2006
Loading...

Share This Page