0

Given a list of process names, I want those parent/child processes to not appear in the output of a ps --forest call.

This will reduce the processes that I need to look at when I'm tracking things.

2
  • Hi, Jeff. Sorry if my wording was unclear. I want parent/child processes of processes that are NOT part of the list I provide. I'm trying to exclude parent/child processes of processes that I know I do not want to look at in my output.
    – lukemk1
    Nov 8, 2018 at 17:06
  • What do you mean by "a list of process names"? Do you mean a list of pids, or a list of commands? What is a "process name"? Nov 8, 2018 at 18:12

1 Answer 1

1

Here's a hard way to do it: a function that will use pgrep to return pids -- excluding (-v) the given process names -- followed by a ps call that asks for a forest listing of only the desired pids:

function psexclude {
  case $# in
  (0)
        printf "Usage: psexclude procname1 ...\n" >&2
        return 1
        ;;
  (*)
        str=$(IFS='|'; printf '%s' "$*")
        wanted=( $(pgrep -fv -- "$str") )
        ;;
  esac
  ps --forest -p "${wanted[@]}"
}

For a shell without array support, consider an alternative:

#!/bin/sh

function psexclude {
  case $# in
  (0)
        printf "Usage: psexclude procname1 ...\n" >&2
        return 1
        ;;
  (*)
        str=$(IFS='|'; printf '%s' "$*")
        set -- $(pgrep -fv -- "$str")
        ;;
  esac
  ps --forest -p "$@"
}
8
  • this is exactly what I needed! I just have one more request: how can this be made to work within a legacy, sh only environment?
    – lukemk1
    Nov 8, 2018 at 20:31
  • @lukemk1, try the alternative I just added, and see!
    – Jeff Schaller
    Nov 8, 2018 at 20:42
  • thanks for this correction! It does function in pure sh now, but it is not excluding anything given to it. I have copied some test output to pastbin here
    – lukemk1
    Nov 8, 2018 at 21:02
  • Looks to me like too much quoting-- drop the double quotes around the process names, since they're not needed.
    – Jeff Schaller
    Nov 8, 2018 at 21:07
  • 1
    yes sorry I wasn't able to get back to you sooner. This now works exactly as I was wanting! Thank you very much for all of your help, I appreciate it!
    – lukemk1
    Nov 12, 2018 at 17:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .