LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 10-26-2007, 09:10 AM   #1
RaelOM
Member
 
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.

Code:
#!/bin/bash

#########################
# 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.
# runaway.sh will not evaluate anything that is not owned by an application ID.

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

# Find processes older than 24 hours

for i in `cat /tmp/glist `; 
do 
	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;
done

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

for i in `cat /tmp/klist`; 
do 
	echo "`date +%b" "%d" "%T` `hostname` runaway.sh: 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; 
done

#Cleanup
rm /tmp/glist
rm /tmp/klist
 
Old 10-26-2007, 09:26 AM   #2
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 10, Linux Mint 11
Posts: 148

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.

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

Rep: Reputation: 62
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:
Code:
for f in *; do 
    ....
done
OR this:
Code:
for f in *
do 
    ....
done
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.
 
  


Reply


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


All times are GMT -5. The time now is 04:35 PM.

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