Menu

#3888 extra greps are extra

open
5
2011-04-11
2011-04-11
Anonymous
No

The default find_pid_command is always something along the lines of:
ps auwwwx | grep NAME | grep -v grep | awk '{ print $2 }'
However, there's little reason to use the two greps when one could use an awk variable within the awk that's already launching anyway:
ps auwwwx | awk -v "n=/NAME/" '$0~n{ print $2 }'

This structure should work on all supported platforms, after making the same per-OS adjustments to different ps commands or different fields printed. Moving to an awk statement further opens up the possibilty of checking a specific field rather than grepping the entire line. Using double-quotes around the variable

This is clearly a minor issue with an associated minor performance improvement, but it also resolves the [also minor] problem of not being able to find the pid for grep. :)

Discussion

  • Anonymous

    Anonymous - 2011-04-11

    Forgot to append:

    Using double-quotes around the "n=NAME" will only work if NAME doesn't contain spaces. I suppose the "right" way to do it with the existing code would be to leave the quotes off and use
    -v n=NAME

     
  • Jamie Cameron

    Jamie Cameron - 2011-04-11

    Yeah, this is going to be tough to co-ordinate with a code change as the find_pid_command will not get changed in an upgrade.

    Also, in 99.9% of cases Webmin will never even run this command - instead it either looks in /proc or calls the Running Processes (proc) module to fetch the full process list and then does its own filtering.

     

Log in to post a comment.

Auth0 Logo