Running processes

pshell.find_procs_by_cmdline(*cmdlines)

Search all processes that have a partial match for at least one of the given command lines. Command lines are parsed through resolve_env().

For example, the command:

find_procs_by_cmdline('$MYROOT')

will return a match for the following processes:

  • $MYROOT/static/scripts/something.sh
  • tail -f $LOGDIR/mylog.log
  • myservice.sh -c /algodata/someuser/root/cfg/myservice.cfg

where:

  • MYROOT=/algodata/someuser/root
  • LOGDIR=/algodata/someuser/root/log

This method will only return processes for the current user.

Warning

Invoking this with relative paths can give erroneous results. For example, invoking it with ‘foo’ will match, for example, ‘foo.pl’, ‘find_foos.sh’, and ‘vim foobar.cfg’.

Warning

This command can’t match commands invoked with a relative path if the search parameter is an absolute path. e.g. find_procs_by_cmdline('$MYROOT') won’t be able to match cd $MYROOT/bin && ./myscript.

Parameters:cmdlines – one or more paths command lines to search for
Returns:list of psutil.Process objects
pshell.kill_procs(procs, *, term_timeout=10)

Send SIGTERM to a list of processes. After term_timeout seconds, send SIGKILL to the surviving processes.

This function will return before term_timeout if all processes close themselves following SIGTERM.

This function graciously skips processes that do not exist or for which the user doesn’t have enough permissions. It also automatically skips the current process and its parents.

Parameters:
  • procs – sequence of psutil.Process objects, e.g. as returned by find_procs_by_cmdline().
  • term_timeout (float) – seconds to wait between SIGTERM and SIGKILL. If term_timeout==0, skip SIGTERM and immediately send SIGKILL.