How to search for running processes on Linux

An AI image.
An AI image depicting the time when I wondered if I would ever find the python processes on my Linux computer.

I made a bash function that searches for processes by COMMAND and ARGS. It does substring matches on those.

pss() {
    local usage
    usage="Usage: pss 'CMD' 'ARGS'
    Search processes matching CMD and ARGS substrings.
    Ex: pss python 'http.server 23456'
    Excludes: awk and defunct processes
    
    Output: PID cmd args...
    
    Returns:
       0 on success
       1 on failure
       2 on usage error"

    if [[ $# -ne 2 ]]; then
        echo "$usage" >&2
        return 2
     fi
    local result
    result=$(ps -w -w -eo pid,stat,cmd,args \
        | awk '$2 !~ /Z/ && $3 !~ /awk/' \
        | awk -v cmd="$1" -v args="$2" \
            '$3 ~ cmd && \
            substr($0, index($0, $4)) ~ args { print $0 }')
    if [[ -n "$result" ]]; then
        echo "$result"| awk '{$2=$4=$5="";print}'
        return 0
     else
        return 1
     fi
 }

This is mainly for finding programs that are being run by one of the scripting languages (bash, python, perl). It lets you first search by the command name before searching by the args.

An even better way is to write your programs so that they allow the user to give the program a unique id from the command line. The program doesn't even have to do anything with this internally. The user can then do a process search for the unique id using ps and grep, while making sure to filter ps and grep from the results.

So I did this and it changed my life.

Comments are turned off.