Leftover process "mpv", i want to kill it automatically, can i do it based on process state code?

,

Hello, on Debian 11 I have noticed increased CPU usage and using “ps aux” found:

BAD PROCESS:

user 6512 0.0 0.0 707020 2392 ? SLl Jun05 0:00 /usr/bin/mpv --idle --no-terminal --force-window=no --ontop --audio-display=no --keep-open=no --no-config --include=/home/user/.local/share/Anki2/mpv.conf --input-ipc-server=/tmp/mpv.5nkoxse1
user 1298251 244 0.0 2987960 19176 ? RLl Jun08 2139:11 mpv --player-operation-mode=pseudo-gui – /path/file.0.part

I have seen no open MPV and i believe i have closed the player after playing that file, so it seems like a bug so i have terminated that process that seems to be stuck for days.

And tried to open the mpv and compare the “ps” output in order to find the difference and automatically terminate “erroneous/leftover” process.

GOOD PROCESS

user 6512 0.0 0.0 707020 2392 ? SLl Jun05 0:00 /usr/bin/mpv --idle --no-terminal --force-window=no --ontop --audio-display=no --keep-open=no --no-config --include=/home/user/.local/share/Anki2/mpv.conf --input-ipc-se
rver=/tmp/mpv.5nkoxse1
user 3024799 15.7 0.2 4527560 151216 pts/2 SLl 05:54 0:00 /usr/bin/mpv --player-operation-mode=pseudo-gui – /path/file.mp4

-→ so the difference is that the bad process has process state R (running or runnable (on run queue))

           D    uninterruptible sleep (usually IO)
           I    Idle kernel thread
           R    running or runnable (on run queue)
           S    interruptible sleep (waiting for an event to complete)
           T    stopped by job control signal
           t    stopped by debugger during the tracing
           W    paging (not valid since the 2.6.xx kernel)
           X    dead (should never be seen)
           Z    defunct ("zombie") process, terminated but not reaped by its parent

When i launch mpv process and play or pause, it always has SLl (Sleeping State: Interruptible (S)). But when it had RLl, the mpv process was NOT running on foreground and so looked like to be erroneously stuck eating CPU time. Definition of the “R” state process: “When a new process is started, it’ll be placed into the running or runnable state. In the running state, the process takes up a CPU core to execute its code and logic.

So maybe i can kill mpv on Linux in case its “PROCESS STATE CODE” is RLl and not any other (like SLl)?

I would use kill command similar to this:

if [[ $(pgrep -f “mpv --player-operation-mode=pseudo-gui”) ]]; then timeout -vk 5 7 kill $(pgrep -f “mpv --player-operation-mode=pseudo-gui”); fi

Following cronjob has been tested to kill the “mpv” process in case its state is R(running or runnable (on run queue)) or Z(zombies would not be killed i presume):

* * * * * pgrep -l mpv | while read pid pname; do ps -p $pid -o state= | grep -qE "R|Z" && timeout -vk 5 7 kill "$pid"; done 1>/dev/null

Mine mentioned cronjob wrongly kills mpv even during regular playback. There is a command that should kill it based on CPU load:

* * * * * ps -u user -o pid= -o pcpu= -o cmd= | awk '$2 > 200' | awk '$3 ~ /mpv/ {print $1}' | xargs -I {} kill {} # kill mpv in case it has 200 CPU overload

I still suspect it may not be fully reliable to solve the issue.