findproc

This is a utility I wrote, that lets you find processes in Linux according to specified criteria, with usage syntax analogous to that of the 'find' command. I wrote it because otherwise you tend to end up doing text parsing of 'ps' output, which gets rather messy.

Usage examples

Here are some examples, to give an idea of what you might use it for. At a guess, the examples will give you the idea whether the utility is likely to be useful to you, though for a full list of options see the full usage message below.

findproc
write PIDs of all user processes
findproc -user root -full
give full info on all root-owned processes
findproc -cmdline-regexp foo -not -this -kill KILL
send KILL signal to all processes with 'foo' in the command line, apart from the findproc process itself
findproc -not \( -user root -or -mine \) \( -utime +5m -nice 0 -or -size +100M \)
find all processes not owned by either yourself or root, that have a user CPU time of five minutes or more and are not niced, or that have a virtual size of 100MB or more

To obtain and run


Full usage message

findproc  -- finds processes under Linux according to specified criteria, 
             and optionally performs actions


  Usage:            findproc [tests] [actions]
  ------

          OR:       findproc -help    -- for this help message
                    findproc -h

   The usage is designed to be similar to that of the 'find' command which is
   used for finding files.  See examples below.

     tests:
     ------
          
           -pid NUM                test numeric PID of process
           -nice NUM               test nice value of process

           -suspended              true if process is in suspended ("T") state
           -running                true if process is in running ("R") state
           -zombie                 true if process is in zombie ("Z") state
           -sleeping               true if process is in sleeping ("S") state
           -paging                 true if process is in paging ("W") state
           -uninterruptible        true if process is in uninterruptible ("D")
                                      state
                                   
           -mine                   true if process is owned by invoking user
           -uid NUM                test numeric UID owning process
           -user STR               test user name owning process
                                   
           -gid NUM                test numeric GID owning process
           -group STR              test group name owning process
                                   
           -age TIME               test age of process
           -utime TIME             test user CPU time of process
           -stime TIME             test system CPU time of process
                                   
           -size SIZE              test virtual size of process
           -resident SIZE          test in-core size of process
                                   
           -command STR            test command name of process
           -command-regexp REGEXP  test command name of process using regexp
                                     test
           -cmdline-regexp REGEXP  test entire command line of process using
                                     regexp test

           -this                   true if process is the findproc itself


     In all the above,

         NUM is a positive number, optionally preceded by a "+" for a "greater
             than or equal to" test or a "-" for a "less than or equal to" test

         TIME is a value which supports the same prefixes as NUM,
              and also supports the suffixes "s" / "m" / "h" / "d" (for
              seconds, minutes, hours, days), defaulting to seconds

         SIZE is a value which supports the same prefixes as NUM,
              and also supports the suffixes "b" / "k" / "m" / "g" or
              upper-case eqivalents (for bytes, kb, Mb, Gb); defaults to bytes

         STR  is a string for a direct equality test only

         REGEXP is a regular expression understood by Python's "re" module;
                NB often these will need quoting in the invoking shell


     Tests can be combined with operators (decreasing precedence):
           ( EXPR )
           -not EXPR
           EXPR -and EXPR,  or equivalently  EXPR EXPR
           EXPR -or EXPR
           
           (note: parentheses will need quoting or escaping in most shells)


    Actions:
    --------

       -print        Print the process ID of matching process

       -full         Print full information for matching processes

       -kill SIGNAL  Send matching processes the named signal.  SIGNAL can be
                     numeric (e.g. "9") or name (e.g. "KILL" or "SIGKILL")

       -renice LEVEL  Sets process priority to LEVEL, e.g. "-renice 19" for
                      lowest priority
       

    Examples:
    ---------
           findproc

                write PIDs of all user processes
 
           findproc -user root -full

                give full info on all root-owned processes

           findproc -cmdline-regexp foo -not -this -kill KILL

                send KILL signal to all processes with 'foo' in the command
                line, apart from the findproc process itself

           findproc -not \( -user root -or -mine \)   \
                       \( -utime +5m -nice 0 -or -size +100M \)

                find all processes not owned by either yourself or root, that
                have a user CPU time of five minutes or more and are not niced,
                or that have a virtual size of 100MB or more