Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Equal strings don't match!

by kyle (Abbot)
on Jul 30, 2008 at 18:28 UTC ( [id://701212]=note: print w/replies, xml ) Need Help??


in reply to Equal strings don't match!

On my system, stuff read from /proc/*/cmdline comes complete with a trailing null character (chr 0) and also nulls separating the command from its arguments. I might rewrite your code this way:

use strict; use warnings; opendir my $proc_dh, '/proc' or die "Can't opendir '/proc': $!"; my @every_pid = grep /\d/, readdir $proc_dh; closedir $proc_dh; chomp @every_pid; my @every_cmd; foreach my $pid ( @every_pid ) { open my $cmdline_fh, '<', "/proc/$pid/cmdline" or die "Can't open '/proc/$pid/cmdline': $!\n"; my ($cmd) = split /\0/, scalar <$cmdline_fh>; push @every_cmd, $cmd if defined $cmd; close $cmdline_fh; } if ( grep { '/usr/sbin/sshd' eq $_ } @every_cmd ) { print "found\n"; } else { print "not found\n"; }

Notice that,

I would point out also that you may be able to say "ps -C sshd" at your command prompt and find out what you want.

Replies are listed 'Best First'.
Re^2: Equal strings don't match!
by pl (Initiate) on Jul 30, 2008 at 18:44 UTC
    Thanks,but this is just a small part of my program,I need to get the values exactly like they are or I will need to change other parts of the program. I need to know why they don't match, because they look the same! Even with chomp the code doesn't work(I have already checked if the cmdline has newlines, but it doesn't)! You think that if I remove the null character(\0) in all elements of @CMD, it will work?

      The null character does seem to be the only difference between what you have in @CMD and the literal string. Have you tried removing it? You could remove nulls from everything in that array this way:

      tr/\0//d for @CMD;

      That means, however, that a command line with several arguments (e.g., "echo one two") will have those arguments all stuck together (e.g., "echoonetwo") instead of separated with nulls.

      Without knowing what you're really trying to do, it's hard to give good advice. See XY Problem.

        The programm is working now! I developed a monitoring application for linux. I have other arrays with info about each process that I get from the proc fs...This might help to understand:
        sub get_data_from_proc_fs{ opendir(PROC,"/proc"); @PROC = readdir PROC; closedir(PROC); @pid = grep /[0-9]/, @PROC; for($k=0;$k<@PID;$k++){ open(STAT,"<","/proc/$PID[$k]/stat"); ($STAT) = <STAT>; push(@app,(split(" ",$STAT))[1]); push(@pgrp,(split(" ",$STAT))[4]); close(STAT); open(CMDLINE,"<","/proc/$PID[$k]/cmdline") ; ($CMDLINE) = <CMDLINE>; push(@cmd,$CMDLINE); close(CMDLINE); } $/ = "\0"; chomp(@cmd); $/ = "\n"; } #####some code here# &get_data_from_proc_fs ; &get_system_date ; $a = 0 ; $PROC_FS_NEW = 0 ; while( $a < $PS_MONITOR ){ $b = 0 ; $found = 0 ; while( $b < $PS_PROC_FS ){ if( ($cmd_monitor[$a] eq $cmd[$b]) && ($pgrp[$b] eq $pid[$b]) +){ if( $first_time == 1 ){ $status = "NORMAL" ; $process = "${app[$b]}_${pid[$b]}" ; &print_status_info($process,$cmd[$b],$pid[$b]) ; $found++ ; } &save_info_about_running_processes_to_monitor ; $PROC_FS_NEW++ ; } $b++ ; } if(( $found == 0 ) && ( $first_time == 1 )){ $status = "ERROR" ; &print_status_info($app_m[$a],$cmd_m[$a]) ; } $a++ ; #####code continues...#
      Problem resolved! It was the null character(\0)...I used: $/ = "\0"; chomp(@CMD); Cheers, fl.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://701212]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2025-07-14 12:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.