Linux – How to find/kill a specific python program


There are two different python programs running in this VM

one is a background job who monitors a folder and then 'does stuff' (with several workers)

10835 ?        Sl     0:03 python
10844 ?        Sl    34:02 python
10845 ?        S     33:43 python

the second one is started via script

20056 pts/1    S+     0:00 /bin/bash ./
20069 pts/1    S+     0:00 /bin/bash ./
20087 pts/1    S+     0:10 python /home/path/

i have tried numerous things to find a way to kill only the main program (i want to build a cron watchdog), but non succeded

first one i want to find only the hanging 'python' process (accompanied by [defunct]), but i cant even find just this process alone.

the upper ones are from ps -ax (so they both are running currently)
pgrep 'python' returns all PIDs, also from which i dont want: (not usefull, so is pkill therefore)

pgrep 'python'

pgrep 'python' always returns empty, so does pgrep ';

the only thing which works

ps ax | grep 'python'

but this one also returns its own PID, grepping 'ps' isn't a prefered solution afair. when hangs, it shows "python [defunct]". a

ps ax | grep 'python [defunct]'

is useless as test as it always returns true. pgrep for anything more than 'python' also returns always false. i am a bit clueless.

Best Solution

This works for me. Found it on the pgrep bro pages.

Find the pids of processes with '' as an argument, like 'python'

pgrep -f

And I use it to check if a python process is running:


if pgrep -f "$searcher" > /dev/null
    echo "$(date +%y-%m-%d-%H-%M-%S) $searcher is alive. Doing nothing."
    echo "No $searcher. Kickstarting..."
    pushd $HOME/there/;
    echo "Pgrepping $searcher:"
    pgrep "$searcher" # out to loggers