LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Monitor Process and restart if not Running (https://www.linuxquestions.org/questions/linux-newbie-8/monitor-process-and-restart-if-not-running-700367/)

Rilly 01-27-2009 08:21 PM

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?

auximini 01-27-2009 08:48 PM

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.

Rilly 01-27-2009 09:24 PM

Thanks - I'll give that a try with some testing

Rilly 01-28-2009 06:33 PM

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


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