Ask a Question related to PERL Miscellaneous, Design and Development.
-
Hugh Kang #1
kill command in a perl script
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 Guest
-
command line commands passed to perl script?
Hi all, Sorry, should have added this to my last email. Does anyone know how to pass values to a perl script through the command line? Do you... -
problem using bash variables with command-line perl in bash script
Hi! I have a problem with variables when using command-line perl in a bash script. The script should update a date (in 2003-10-10 form) if the... -
Using the shell's "fmt" command in a perl script
I have a variable containing about 200 characters of text, named $text. I would like to run the shell command "fmt" against this text, and print... -
Passing parameters to a Perl Script from Command Line (Linux)
Hi, I'm fairly new to both programming in shell script (linux) and in programming in perl. I need to pass a parameter to the perl script from... -
Using perl/shell scripts to kill processes more than x days old
Hi Folks, OS : Solaris 8 I have a question regarding killing processes on a unix server. I would like to run a cron job to kill processes run... -
Ted Zlatanov #2
Re: kill command in a perl script
On 15 Sep 2003, [email]skang@leaguedata.com[/email] wrote:
Use Proc:ProcessTable from CPAN.> 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.
<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 Guest
-
Jim Gibson #3
Re: kill command in a perl script
In article <257137ea.0309150913.11f08d49@posting.google.com >, Hugh Kang
<skang@leaguedata.com> wrote:
The above line is unnecessary because of the following line.> 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);
kill 3, $pid_comp[0]> ($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?
See 'perldoc -f kill'
Not easily, and not necessary because of answer to 1.> 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]
Yes. You can get the filtered output of the ps command using backtics:>
>
> Q3) Any other way to get what I want?
[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]
Hope I have.>
>
> Can anyone help me out with this issue please?
>
> Many thanks in advance!
>
> HughJim Gibson Guest
-
Hugh Kang #4
Re: kill command in a perl script
Jim Gibson <jgibson@mail.arc.nasa.gov> wrote in message news:<150920031752553144%jgibson@mail.arc.nasa.gov >...
> In article <257137ea.0309150913.11f08d49@posting.google.com >, Hugh Kang
> <skang@leaguedata.com> 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 Guest
-
Jim Gibson #5
Re: kill command in a perl script
In article <257137ea.0309160547.3d14a032@posting.google.com >, Hugh Kang
<skang@leaguedata.com> wrote:
You can filter out the grep process with> Jim Gibson <jgibson@mail.arc.nasa.gov> wrote in message
> news:<150920031752553144%jgibson@mail.arc.nasa.gov >...>> > In article <257137ea.0309150913.11f08d49@posting.google.com >, Hugh Kang
> > <skang@leaguedata.com> 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.
@lines = `ps -ef | grep java | grep -v grep'
or you can skip it in the loop:
if( /grep/ ) {
# skip this line
}else{
my ($user,$pid) = split;
...
or make that a one-liner:
push(@pids,(split)[2]) unless /grep/;
You can extract the 11th column with $it11 = (split)[11]; or just check>
> 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?
>
for the presence of the '-Xms1024m' anywhere in the line with if(
/-Xms1024m/) {...]; or put back all of the other column variables in
the split: ($user,$pid1,$pid2,$it4...,$it11) = split;
Read the log file within the Perl program and check for the error. If> 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?
you have a program to check the log file that returns an exit value,
run the program with the system function and capture the return value
(a little tricky -- see perldoc -f system).
>
> I just ordered Perl manuals to study and practice more.
>
> Thanks again.
>
> HughJim Gibson Guest
-
John W. Krahn #6
Re: kill command in a perl script
Hugh Kang wrote:
my @lines = grep /java/, `ps -ef`;>
> 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.
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`;
My version of "ps -ef" doesn't have eleven columns> 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?
kill 'QUIT', grep { /-Xms1024m/ and $_ = (split)[0] } `ps h -C java -o
pid,command`;
You do realise that using a negative number with kill is different then> q3) kill -3 $pid is not killing the process.
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 Guest
-
Ted Zlatanov #7
Re: kill command in a perl script
[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 Guest
-
John W. Krahn #8
Re: kill command in a perl script
Ted Zlatanov wrote:
$ cat Proc-ProcessTable-0.38/README>
> [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.
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 Guest
-
Barry Kimelman #9
Re: kill command in a perl script
[This followup was posted to comp.lang.perl.misc]
In article <257137ea.0309150913.11f08d49@posting.google.com >,
[email]skang@leaguedata.com[/email] 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 Guest
-
Daniel Berger #10
Re: kill command in a perl script
"John W. Krahn" <krahnj@acm.org> wrote in message news:<3F680157.65BFBE0C@acm.org>...
Dan should really remove that. His code has been around long enough,> 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]
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.
It requires a C compiler - so what? If you're on *BSD or Linux,> 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
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.
Hmm....I don't remember this code, so I can't say much about it. The> 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?
author, Dan Urist, would probably be willing to explain this code if
you ask him.
Your choices are to parse 'ps' (blech) or use Proc::ProcessTable. As> Not that I'm against using modules. :-) I just like to know what all
> my options are.
>
>
> John
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 Guest
-
Bart Lateur #11
Re: kill command in a perl script
Ted Zlatanov wrote:
Indeed. The reason is simple: wraparound beyond a fixed number of bits>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.
for the ID numbers. So after a while they start back at zero. (Or 1...?)
--
Bart.
Bart Lateur Guest
-
Ted Zlatanov #12
Re: kill command in a perl script
On Wed, 17 Sep 2003, [email]krahnj@acm.org[/email] wrote:
I absolutely agree. In this case, your options are to either use> Not that I'm against using modules. :-) I just like to know what
> all my options are.
Proc::ProcessTable or to write alpha-quality software. Using ps -ef
and trying to manually parse the output is an absolutely horrible
solution, and please understand I say that because I've done it, not
to criticize your efforts.
Ted
Ted Zlatanov Guest



Reply With Quote

