linuxhw / hw-probe

Probe for hardware, check operability and find drivers
https://linux-hardware.org/?view=timeline
Other
735 stars 62 forks source link

Searching for running processes with "ps x | grep" is wrong #119

Closed danfe closed 2 years ago

danfe commented 2 years ago

Results would contain the grep(1) line itself (with arguments), so this construct will always find what's being searched. Also, there's needless use of pipe. Consider the following patch:

-        my $WMs = runCmd("ps x | grep wm");
+        my $WMs = runCmd("pgrep -l wm");
         if($WMs=~/\s(\w+wm)/) {
             $Sys{"Current_wm"} = $1;
         }

-        if(runCmd("ps x | grep start-hello"))
+        if(runCmd("pgrep -l start-hello"))

Now the probe correctly detects my IceWM instead of bogusly reporting that I run helloSystem DE.

linuxhw commented 2 years ago

pgrep -l start-hello returns empty output on helloSystem.

helloSystem-0

linuxhw commented 2 years ago

Fixed by commit 1274919ef786285bbdcf14329413d7545e68e433

danfe commented 2 years ago

That's because it's not part of the process name (/bin/sh) but its arguments. For that you'd need to use -f switch. From man pgrep(1):

-f Match against full argument lists. The default is to match against process names.

Committed fix is ugly, and now requires two pipes while it can use none.