Lowyat.NET Forums

Welcome Guest ( Log In | Register )

 
RSS feedReply to this topicStart new topicStart Poll

Outline · [ Standard ] · Linear+

> Need a script to autorestart crashed program

aMer
post Aug 23 2008, 03:37 PM
Show posts by this member only |Rating BETA (0+, 0-) | Post #1


Regular
**

Group: Junior Member
Posts: 195
Joined: January 2006





Hi guys. Can anyone here write a simple bash script with loop to check if the executed process is still running. If it's not then auto restart the program. Yea a simple script. Please don't suggest me to use other tool etc.

the script should work like this:

user runs the script-->script executes the program and grab its process pid-- > loop (every 60 secs): check if the program pid still exist/crash


just like that. hmm i'll give more details.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
oshiri
post Aug 23 2008, 09:27 PM
Show posts by this member only |Rating BETA (0+, 0-) | Post #2


Enthusiast
****

Group: Senior Member
Posts: 585
Joined: November 2004




Try this stupid script. Created a few minutes ago on Debian to check apache2 service:

CODE
#!/bin/bash
# created by mambang

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
APP="/etc/init.d/apache2" # change this
NAME="apache2" #change this
APPOPTION="start"  # optional
PGREP="/usr/bin/pgrep"  # depends on pgrep for checking pid
DATE=`date +"%d-%m-%Y"`  #optional for logging


function start(){
$PGREP ${NAME} > /dev/null
if [ $? -ne 0 ]
then
       echo "$DATE : Pid $NAME tiada. $NAME dimulakan..."
       exec $APP $APPOPTION | tido
else
       echo "$DATE : $NAME sedang run ...."
       tido
fi
}

function tido(){
sleep 20s #change this to 60s if you want to check every 60 seconds
start
}

start >> /var/log/checkpid &
exit


This script will send output to /var/log/checkpid.

CODE
# tail -f /var/log/checkpid
23-08-2008 : Pid apache2 tiada. apache2 dimulakan...
23-08-2008 : apache2 sedang run ....


update: date does not work..hehe

This post has been edited by oshiri: Aug 23 2008, 09:45 PM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
wKkaY
post Aug 24 2008, 12:45 AM
Show posts by this member only |Rating BETA (0+, 0-) | Post #3


Crashing like a tidal wave..
Group Icon

Group: Forum Admin
Posts: 4,021
Joined: January 2003
From: i-city




You can also try something ready-made like monit or daemontools.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
oshiri
post Aug 24 2008, 08:39 AM
Show posts by this member only |Rating BETA (0+, 0-) | Post #4


Enthusiast
****

Group: Senior Member
Posts: 585
Joined: November 2004




Has to agree.
I used monit before and it's very good. You can monitor all services you set using web.
It will alert(mail) you everytime service down and will restart it automatically.

The only problem I had with monit is sometime I can't access the web page.
Don't know why, it's port got close after few days by firewall. That's not really a big problem, you can just leave monit alone, it still doing it's job.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
aMer
post Aug 24 2008, 08:24 PM
Show posts by this member only |Rating BETA (0+, 0-) | Post #5


Regular
**

Group: Junior Member
Posts: 195
Joined: January 2006





hai bro. thanks. i haven't tested it out yet but from the codes i think it should work almost like i want

i just need few small changes in the codes..

1. write log only when program crashed
2. in log details, include both date & time
3. check for 2 pid's since my program will launch program B to run along with it. if both pid are not exist, then rerun the program specified in $NAME


and yeah monit is pretty cool but it's way overkill for simple task like this tongue.gif i prefer something more simple like script

This post has been edited by aMer: Aug 24 2008, 09:16 PM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
oshiri
post Aug 24 2008, 08:58 PM
Show posts by this member only |Rating BETA (0+, 0-) | Post #6


Enthusiast
****

Group: Senior Member
Posts: 585
Joined: November 2004




1. just remove echo "$DATE : $NAME sedang run ...."
2. you can include date+time like this: DATE=`date +"%d-%m-%Y" + %r`
As I mentioned, date does not work. It log the date but does not update it.
Probably DATE should be move to function start.
3. Just add extra checking in function start. You can use elif
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
aMer
post Aug 24 2008, 10:05 PM
Show posts by this member only |Rating BETA (0+, 0-) | Post #7


Regular
**

Group: Junior Member
Posts: 195
Joined: January 2006





QUOTE(oshiri @ Aug 24 2008, 08:58 PM)
3. Just add extra checking in function start. You can use elif
*



can you please show me the complete code for that line?

This post has been edited by aMer: Aug 24 2008, 10:06 PM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
oshiri
post Aug 26 2008, 01:23 PM
Show posts by this member only |Rating BETA (0+, 0-) | Post #8


Enthusiast
****

Group: Senior Member
Posts: 585
Joined: November 2004




example to check apache2 and dhcp3-server services in Debian:

CODE

#!/bin/bash
# created by mambang

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
APP="/etc/init.d/apache2" #change
NAME="apache2" #change to the name of pid
APPOPTION="start"  #needed only if using startup script in /etc/init.d
APP2="/etc/init.d/dhcp3-server" #change
NAME2="dhcpd3" #name of pid
APPOPTION2="start"  #needed only if using startup script in /etc/init.d
PGREP="/usr/bin/pgrep"  #needed to check pid. Makesure you install.


function check1(){
$PGREP ${NAME} > /dev/null
if [ $? -ne 0 ]
then
DATE=`date +"%d-%m-%Y+%r"`
echo "$DATE : Pid $NAME tiada. $NAME dimulakan..."   #for logging
$APP $APPOPTION >/dev/null
echo
check2
else
DATE=`date +"%d-%m-%Y+%r"`
       echo "$DATE : $NAME sedang run ...."  # for logging. Comment if you don't need it
echo
check2
fi
}

function check2(){
$PGREP ${NAME2} >/dev/null
if [ $? -ne 0 ]
then
DATE=`date +"%d-%m-%Y+%r"`
       echo "$DATE : Pid $NAME2 tiada. $NAME2 dimulakan..." #for loging
$APP2 $APPOPTION2 > /dev/null
echo
tido
else
DATE=`date +"%d-%m-%Y+%r"`
echo "$DATE : $NAME2 sedang run ...."  # for logging. Comment if you don't need it
echo
tido
fi
}

function tido(){
sleep 20s
check1
}

check1 >> /var/log/checkpid &   # start and logging

exit


This post has been edited by oshiri: Aug 26 2008, 01:28 PM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
DarkTenno
post Sep 6 2008, 08:42 PM
Show posts by this member only |Rating BETA (0+, 0-) | Post #9


Regular
**

Group: Junior Member
Posts: 81
Joined: February 2005
From: KL






this script I wrote for my home server and all my machine, a simple script can work with any systems unix/bsd/linux smile.gif

CODE

#!/bin/sh
#
# daemon cron script can be use with anything program that have a pid files
# zuan(at)mylinux.net.my|daemon.cron|200810729|2343
#

daemon_dir="/path/to/your/program"
daemon_exec="program"
daemon_pid="/path/to/your/program.pid"

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

cd $daemon_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 $daemon_pid ]; then
  pid=`cat $daemon_pid`
     if [ `ps -p $pid | wc -l` -eq 2 ]; then
            exit
        else
        echo "Stale PID File"
   fi
fi

echo "Starting Your Deamon Process..."
$daemon_exec
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Bump TopicReply to this topicTopic OptionsStart new topic
 

Lo-Fi Version Time is now: 22nd November 2008 - 11:23 AM
All Rights Reserved 2003-2008 Vijandren Ramadass (~resistance is futile~)