Script to kill matching processes owned by a specific user after a certain time

This Linux Bash shell script will kill matching processes using grep owned by a specific user after a certain amount of time has passed.

It requires the ‘/proc’ file system on Linux and uses the time
stamp on the ‘cmdline’ virtual file which is time-stamped with the process start time.

Specifically, this one kills old OpenOffice processes for a web application
that builds HTML from word documents. MINUTES, PUSER and PMATCH can be changed to suit whatever your process killing needs are.

MINUTES can be changed to DAYS by using find’s -mtime parameter instead of -mmin. ‘man find’ for more info.

All of the binaries are fully pathed for Redhat/Centos (in order to run from cron). You may have to change the
path for some of the binaries.




/usr/bin/find /proc/ 
        -maxdepth 3 
        -name 'cmdline' 
        -user ${PUSER} 
        -mmin +${MINUTES} 
        -exec /bin/grep -l ${PMATCH} {} ; | 
                /bin/cut -d'/' -f3 | 
                /usr/bin/xargs -n1 -i^ /bin/bash -c '/bin/kill ^ 2>&1 > /dev/null'

Tags: , , , , , , , , , , , , , , , ,