Reputation: 613
So my goal is to kill processes that get stuck transcoding media. All processes are monitored by runit so when the process gets killed - it gets restarted.
I can get the list of processes the following way:
ps aux | grep -v grep | grep ffmpeg | awk '{print $2, $3}'
What would I need to have any processes killed that returns <20 on $3 - in other words, process is using less then 20% of CPU?
Upvotes: 0
Views: 601
Reputation: 7959
Another way of doing this:
ps axo comm,pid,pcpu | awk '/^ffmpeg/ && $3 < 20 {print $2}' | xargs -r kill -9
Upvotes: 0
Reputation: 8412
Another approach using using awk without grep
ps aux|awk '{if($11~ "ffmpeg" && $3<20.0){system("kill -9 "$2)}}'
($11~ "ffmpeg" && $3<20.0) # if field $11 (COMMAND column) matches "ffmpeg" and field $3 (PID column) is less than 20.0, kill PID no. which is in field $2
Upvotes: 1
Reputation: 1
pgreg(1) can give you a list of pids. So pgrep ffmpeg
will give you a list (like e.g.2345 15678 9870
) of pids of processes running ffmpeg
. Hence ps u $(pgrep ffmpeg)
gives you a process list.
You want to filter those processes running for less than 20% of CPU. Try
ps u $(pgrep ffmpeg) | awk '{if ($3 < 20.0) { print "kill " $2 }}'
This should give you several lines like kill 12345
. Feed them to a shell:
ps u $(pgrep ffmpeg) | awk '{if ($3 < 20.0) { print "kill " $2 }}' | sh
You probably could use pkill(1) and GNU awk function system. You may want to skip the title line output by ps u
perhaps by giving also /USER/{next}
to awk
Upvotes: 1