LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-27-2009, 09:21 PM   #1
Rilly
LQ Newbie
 
Registered: Jan 2009
Posts: 3

Rep: Reputation: 0
Monitor Process and restart if not Running


I'm pretty new to linux - and my scripting is useless...

I have multiple processes that run.. but they are the same process, but with different configuration files.

./iroffer name.config (its an XDCC bot)...

I've googled and read several scripts that monitor a process then restart it if its crashed, but I can't relate it to what i need... because i need the config file specific to the one that crashed. I will eventually have maybe up to 10 of these processes running with different name.config parameters (name being the bot name)..

This is very similar to the eggdrop cron job.. but that was way to confusing for me.. Can anyone help me?

I'm thinking cron job entry is what is needed.. run every 15 - 30 minutes maybe.. but the scripting is beyond me

anyone familiar with what I need?
 
Old 01-27-2009, 09:48 PM   #2
auximini
Member
 
Registered: Dec 2003
Location: Calgary, AB
Distribution: Any!
Posts: 146

Rep: Reputation: 18
When a command fails, the shell returns a non-zero error code. So if you were to type this on the command line:

Code:
ps ax | grep "iroffer name.config" | grep -v grep
echo $?
and iroffer was running like it should, then $? would equal 0. If it was not running, it would equal 1. So a rough script would be something like:

Code:
#!/bin/sh

ps ax | grep "iroffer name.config" | grep -v grep
if [ $? -ne 0 ]
then
    # this is just an example
    /etc/init.d/iroffer start
fi
You could then run something like that periodically to make sure it's running.

Make as many of those as you need for however many different variations of iroffer you have running.

Also, test to make sure grep returns the correct matches. I am just assuming it would work in my example.

Hope that helps or at least gets you started.
 
Old 01-27-2009, 10:24 PM   #3
Rilly
LQ Newbie
 
Registered: Jan 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks - I'll give that a try with some testing
 
Old 01-28-2009, 07:33 PM   #4
Rilly
LQ Newbie
 
Registered: Jan 2009
Posts: 3

Original Poster
Rep: Reputation: 0
well. now I feel kinda foolish - after like a week of trying to find a script.. i found out the program comes with a cron job to enter already

i enter this on crontab -e

*/5 * * * * /home/username/iroffer/filename.cron

so it runs every file minutes and it checks if the pid is stale or not.. and restarts it if it is..

So.. now my question - if i had 10 bots.. and this was running every five minutes x 10 times - could this impact performance on a dual core server with 2 GB of ram?

this is the script

Quote:
#!/bin/sh

# --- How To Use Cron ---
#
# Edit the iroffer.cron file's iroffer_dir, iroffer_exec, and
# iroffer_pid variables
#
# then crontab -e and place the following line in the editor
#
# */5 * * * * /full/path/to/iroffer/iroffer.cron
#

iroffer_dir="/full/path/to/iroffer/"
iroffer_exec="./iroffer -b sample.config"
iroffer_pid="mybot.pid"


#### don't touch below here ####

cd $iroffer_dir

# make sure filesystem isn't full
freespace=`df -k . | tail -1 | awk {'print $4'}`
if [ $freespace -lt 10 ]; then
echo "Filesystem Full!"
exit
fi

# see if stale pid file
if [ -f $iroffer_pid ]; then
pid=`cat $iroffer_pid`
if [ `ps -p $pid | wc -l` -eq 2 ]; then
exit
else
echo "Stale PID File"
fi
fi

echo "Starting iroffer..."
$iroffer_exec
 
  


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
Get Process size and Thread count for a particular running process haseit Linux - Newbie 2 01-23-2009 12:09 AM
how can I monitor all pthread status inside a running process valpa Programming 2 08-26-2008 11:54 PM
Shell Script : Kill a running process when another process starts ashmew2 Linux - General 3 08-20-2008 04:47 AM
restart process after dump abselect Linux - General 1 10-18-2006 10:22 PM
restart a process mum_03 Linux - General 7 04-21-2003 03:37 PM


All times are GMT -5. The time now is 08:33 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration