Linux: autokill a crashed / infinite loop process

Sometime a process can take 100% of all available cpu power because it crashed. Here is a little script I used on a server for pdftk because this software was crashing from time to time.

Just launch the script from a cron job and replace “PROCESS_NAME” by the name of the process to watch.

  • argument 1 : number of minutes after which the process will be killed
  • argument 2 : number of seconds
#!/bin/bash

# get process id and execution time
process=`top -b -n 1 | grep PROCESS_NAME | awk '{print $1" "$11}'`

# extract pid
pid=`echo $process | awk '{print $1}'`

# extract execution time
time=`echo $process | awk '{print $2}' | awk -F ":" '{ min=$1; sec=$2; print 60*min+sec}'`

if [ $(echo "$time > 1"|bc) -eq 1 ] ;  then
  kill -9 $pid
  echo "killed process $pid that used $time of processor power"
fi

Questions or comments?

Join the discussion on Discord, ask a question or share your experience.

Discuss on Discord
  1. ChatGPT Image Jan 30, 2026, 07_13_27 PMXMG Pro 16 m25 under Linux: Maximum GPU Power and Gaming Performance
  2. 20250128_224935Cloud at home with minimum toil - PXE / Proxmox / Saltstack / k3s
  3. Banana Pi M1 single-board computerAnother NAS with a BananaPI