Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-26-2007, 09:10 AM   #1
Registered: Dec 2004
Posts: 110

Rep: Reputation: 16
Evaluate my Bash script for runaway proc

Take a look at my script and critique it please.

This script will evaluate a list of ID's and then find processes running under that ID and kill them if they're running longer than 24 hours.


# Name: Runaway Process Killer
# Interval: 24 hours
# Author: Rael Mussell
# Rev: 1.0
# Date: 10/24/2007

#Create list of applications that the runaway scan is ran against.
# will not evaluate anything that is not owned by an application ID.

for i in `ls -d /ford/thishost/unix/cen/ewwwadmin/site*`; 
	grep User $i/conf/httpd.conf | grep -v Log | grep -v name | awk '{print $2}' >> /tmp/glist; 

# Find processes older than 24 hours

for i in `cat /tmp/glist `; 
	ps -e -o user,pid,etime,comm | grep $i| grep -v httpd |grep -v sidd | grep -v 49710 | egrep '([0-9][0-9]*-[0-9][0-9]:[0-9][0-9]:[0-9][0-9])' | awk '{print $2}' >> /tmp/klist;

#Kill processes in klist and place in /ford/thishost/unix/cen/wftools/logs /runaway-kill kill statment

for i in `cat /tmp/klist`; 
	echo "`date +%b" "%d" "%T` `hostname` KILLING PROCESS -- `ps -fe | grep -v grep | grep $i`" >> /ford/thishost/unix/cen/wftools/logs/runaway-kill; kill -15 $i; kill -8 $i; kill -4 $i; kill -2 $i; kill -9 $i; 

rm /tmp/glist
rm /tmp/klist
Old 10-26-2007, 09:26 AM   #2
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150

Rep: Reputation: 18
A suggestion

Instead of using glist and klist for your temporary files, you might want to use something that will be a bit more ... uncommon lets say (if someone else runs a script and use the same file, the second script would likely not work properly as they would not have write permissions to that file. It also frees you up from haveing to remember what file names you are using in /temp should you yourself be running scripts at the same time on the same system). You can use the mktemp to create a unique temporary file in /tmp. Just store it in a variable, and you'll be good to go.

Old 10-26-2007, 09:30 AM   #3
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Using predictable names for your kill list and glist is risky... an attacker could use this to kill off necessary processes on your system.

You don't need all those signals sent by so many kills. It's probably better practice to send SIGTERM and wait a few seconds for programs to terminate gracefully, and then send SIGKILL if they are not dead already. You should check it's the same process, else you might find that a process terminates with SIGTERM, and another (important) process starts with that PID, and then you kill that with SIGKILL which you might not want to do.

The style looks OK. I think it's more typical to not have a ; at the end of the line of you move do to the next line in for statements. i.e. do this:
for f in *; do 
OR this:
for f in *
I don't think it really matters though.

I generally prefer $() to `backticks` to take the output from a program. This is because backticks can be misread as regular quotes with some fonts, and they are not nestable.

In the first for loop, you don't need to use ls to list the files - the pattern itself will be expanded by the shell. You can save yourself a process invocation this way.

Finally, inside the first for loop you are using the value of the i variable - you should quote this in "double quotes". This is because the pattern you use might match files with spaces in them... in this case you have to quote the variable when you use it.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
Boot hang after 'proc on /proc type proc (rw)' Hagoromo Slackware 13 10-05-2007 05:03 PM
Bash script to create bash script jag7720 Programming 10 09-10-2007 07:01 PM
[bash] having trouble debugging this bash script. jons Programming 4 02-08-2007 06:51 AM
Change proc value as a user in a script? Brian1 Linux - General 3 01-26-2005 07:57 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:09 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration