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

Hello Monks, On HP UX, how can get the process id of a process when I've got the name of the process. The system doesn't have /proc directory. Thanks, ICG

Replies are listed 'Best First'.
Re: Getting Process ID from process name
by hubb0r (Pilgrim) on Jun 22, 2005 at 14:51 UTC
    The module Proc::ProcessTable on cpan will give you a hook into the processlist for your OS. As per the README, it does support HPUX:

    Mike Romberg <romberg@fsl.noaa.gov> HPUX port

    I've used this in linux, so cannot vouch for the HPUX support, but it says that it's there.
Re: Getting Process ID from process name
by tlm (Prior) on Jun 22, 2005 at 13:14 UTC

    I don't know about HP UX, but on Linux at least, there's the handy pgrep to do just that.

    the lowliest monk

Re: Getting Process ID from process name
by Joost (Canon) on Jun 22, 2005 at 12:53 UTC
Re: Getting Process ID from process name
by tcf03 (Deacon) on Jun 22, 2005 at 13:23 UTC
    Manipulating the follwoing code should work for you, or at least get you started.
    use strict; use warnings; my @PS_CMD=`ps -e`; my @badpid; my $count=0; $|++; for $_ (@PS_CMD) { chomp; ## get rid of trash ## next if /<defunct>/; s/^\s+//g; s/\s/,/g; s/,$//g; s/^,//g; s/(,)+/,/g; s/\s//g; next unless /^\d+/; ## End of trash collection ## my ($pid, $tty, $time, $cmd) = split(/,/, $_); my ($min, $sec) = split(/:/, $time); # change the $min > ## to change the # threshold at which we will be warned # change $cmd eq "<COMMAND>" to change # the command we "grep" for. Check the # output of `ps -e` for an example. if ( $cmd eq "YOUR_COMMAND" and $min > 200 ) { $count++; push (@badpid, $pid); } } my $pids=join(', ', @badpid); if ( $count > 0 ) { send_mail($pids) }

    I left off the send_mail sub...

    Ted
    --
    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
      --Ralph Waldo Emerson
Re: Getting Process ID from process name
by zentara (Cardinal) on Jun 22, 2005 at 19:03 UTC
    Here is an old snippet that has been passed around here before. I'm sure you could figure out how to grep the array for your $process_name.
    #!/usr/bin/perl my @ps = qx(ps -u $> -o pid,tname,etime,cmd --no-headers); for (@ps) { ($pid, $tname, $etime,$cmd) = unpack "a5 x a11 x a8 x a200",$_; print "$pid $tname $etime $cmd\n"; }

    I'm not really a human, but I play one on earth. flash japh