LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 12-15-2011, 09:12 PM   #1
kalisto
LQ Newbie
 
Registered: Dec 2011
Posts: 2

Rep: Reputation: Disabled
Question Shell Script to Kill and Restart a process based on CPU load


I have been having this problem with a mud i run, anytime it crashes the mud doesn't restart and sometimes it will hang and not allow anyone on, i was wondering if someone could help me tweak my startup script to make it so it kills the process when the cpu load gets around 40%~ and restarts it after killing it

here is what my startup script looks like right now

Code:
#! /bin/csh -f

# Set the port number.
set port = 8100
if ( "$1" != "" ) set port="$1"

# Change to area directory.
cd ~/dbzl/area

# Set limits.
limit coredumpsize unlimited
limit stacksize unlimited
if ( -e shutdown.txt ) rm -f shutdown.txt

while ( 1 )
    # If you want to have logs in a different directory,
    #   change the 'set logfile' line to reflect the directory name.
    set index = 1000
    while ( 1 )
        if ( $index > 1025 ) then
           rm -f ../log/*.log
           set index = 1000
        endif
        set logfile = ../log/$index.log
        if ( ! -e $logfile ) break
        @ index++
    end

    # Record starting time
    date > $logfile
    date > ../area/boot.txt

    # Run DBZL.
    # Check if already running
    set matches = `netstat -an | grep ":$port " | grep -c LISTEN`
    if ( $matches >= 1 ) then
        # Already running
        echo Port $port is already in use.
        exit 0
    endif
    ~/dbzl/src/dbzl $1 >&! $logfile

    # Restart, giving old connections a chance to die.
    if ( -e shutdown.txt ) then
        rm -f shutdown.txt
        exit 0
    endif
    sleep 5
end
Thanks in advance for anyones help
 
Old 12-16-2011, 03:00 PM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
What OS / version?

Just some brainstorming for now:
  • You could make the process less nice(1).
  • You could enforce some limits using pam_limits(8).

I do not like the idea of a kludge that is constantly checking to see if it needs to restart a service. That's a sign that something is very wrong (and should be addressed in a more straightforward way, IMO).
 
Old 12-18-2011, 08:09 PM   #3
kalisto
LQ Newbie
 
Registered: Dec 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Its ubuntu 11.11 and the script wouldn't really bother me, im in the process of using a better codebase to update my work, so hopefully the problem will go away, but in the meantime i want a working script to keep it checking, its quite a big project to undertake and im already running into some walls there, but i will get them worked out eventually.
 
Old 12-20-2011, 06:33 PM   #4
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Try not to take the the wrong way, but - for starters - please script in Bourne shell, and not csh. You'll find more folks have experience with that (for good reason), and will be more able to help.

That said, there may be some difficulties in designing your script. The pseudocode looks something like this:
  1. At regular intervals, check foo process's CPU usage.
  2. If CPU usage < 40%, finish quietly.
  3. If CPU usage >= 40%, kill foo process, and start foo service again.

Is that correct? If so, should foo be killed every time it's observed to be at 40% or above? (For instance, what if there is a very brief spike in CPU usage by foo just at the moment you're polling?)
 
Old 12-21-2011, 05:59 AM   #5
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
I’m wondering, what is meant by 40% in detail. A single process, even with nice 19 can get almost 100% of the CPU time. When there are enough other processes, it might never reach 40% at all, despite the fact that you would like to kill it.
 
  


Reply



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
help w/ script to kill process(es) based on time they've been running. jcardoza Linux - Newbie 4 12-09-2010 11:08 AM
need script to kill process based on STIME wilmomd Linux - General 5 03-12-2010 10:22 AM
Shell Script : Kill a running process when another process starts ashmew2 Linux - General 3 08-20-2008 03:47 AM
kill the process invoked from a shell script, when the script is killed kskkumar Linux - Software 8 05-23-2007 11:29 AM
Shell Script to kill a process automaticaly brazilian_user Programming 11 06-12-2006 09:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:15 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration