Script for Killing Process of Long Running Time

powerdomein

Verified User
Joined
Dec 28, 2005
Messages
121
The following script works at Centos, Fedora:

Make a empty file and call it killlongproc.sh
Copy this:
--------------------

#!/bin/bash
# This script will kill process which running more than X hours
# egrep: the selected process; grep: hours
PIDS="`ps eaxo bsdtime,pid,comm | egrep "spamd|exim|mysqld|httpd" | grep " 1:" | awk '{print $2}'`"

# Kill the process
echo "Killing spamd, exim, mysqld and httpd processes running more than one hour..."
for i in ${PIDS}; do { echo "Killing $i"; kill -9 $i; }; done;

-----------------------

Stop this in your cronjob

15 * * * * * root /{directory}/./killongproc.sh

Have fun!

############## HISTORY ########################
I am trying to write a bash script that kills a (idle) process over 1 hour.
I am doing that because some script hinging up and uses memory.

Thanks a friend a write this one but i dont work.
Can some take a look an help to finish this script.
Also i want make it available in minutes to kill a running/idle process

--------------
#!/bin/bash
PROCS="spamd|exim|mysqld|httpd"

# Show all process which is running more than 00 hours for PROCESS. SO li
PIDS=$(ps eaxo etime,pid,comm | grep "$PROCS" | grep -v " 00:" | awk '{print $2}')

# Kill the process
echo "Killing $PROCS processes running more than one hour..."
kill -9 $PIDS
----------------------------
 
Last edited:
Code:
#!/bin/bash
# Show all process which is running more than 00 hours for PROCESS. SO li
PIDS="`ps eaxo etime,pid,comm | egrep "spamd|exim|mysqld|httpd" | grep -v "00:" | awk '{print $2}'`"
 
# Kill the process
echo "Killing spamd, exim, mysqld and httpd processes running more than one hour..."
for i in ${PIDS}; do { echo "Killing $i"; kill -9 $i; }; done;

Good luck! :)
 
Last edited:
I tried but...
The ps dont work.. I get all PID of the process which i asked..

How can i sort PIDS which running more than one hours?
 
I tried but it didnt work.. It shows still all PID of grep process.
Can you write a PS command which shows PIDS for running over n time?
 
No, it doesn't show all PIDS. Try it by yourself:
All PIDS
Code:
ps eaxo etime,pid,comm

Only spamd, exim, mysqld, httpd:
Code:
ps eaxo etime,pid,comm | egrep "spamd|exim|mysqld|httpd"
 
Ow sorry.. That works fine but my question was:

How can i egrep selected processing running more than 1 hour. ??

this egrep don't work: egrep -v " 00:" OR egrep -v "00:"
When I use it, it shows all PIDS (selected from: egrep "spamd|exim|mysqld|httpd") which already running (over 1 minutes till days) and not running for over an 1 hour.
 
Not egrep, but grep. You can grep "-" or something like that.
 
Code:
#!/bin/bash
# Show all process which is running more than 00 hours for PROCESS. SO li
PIDS="`ps eaxo etime,pid,comm | egrep "spamd|exim|mysqld|httpd" | grep -v "00:" | awk '{print $2}'`"
 
# Kill the process
echo "Killing spamd, exim, mysqld and httpd processes running more than one hour..."
for i in ${PIDS}; do { echo "Killing $i"; kill -9 $i; }; done;

Good luck! :)

Hi,
Nice script, I have one doubt on this.
Is it possible to get output for process command as well?, Can you please share your views
 
Back
Top