LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 05-12-2008, 10:49 PM   #1
sleepykangaroo
LQ Newbie
 
Registered: May 2008
Posts: 5

Rep: Reputation: 0
Bash Help


I have a script that basically monitors a program of your choosing be it bash or binary and when ever it crashes it restarts, if it crashes more then five times it emails the user.
Now the scripts not 100% finished with the email part but my problem is it you run it through interactive mode it runs fine but when you go through CLI (EX: ./runapp logname appname) on the first crash it wont restart and then wont log it.
Here is my code hope you guys can give me a hand
TYIA
-James


Code:
#!/bin/bash



function backgroundchecker {
((count=0))
if [[ $2 == "" ]]
then
	appname=$name
else
	appname=$2
fi

if [[ $1 == "" ]]
then
	errorfile=$errorlog
else
	errorfile=$1
fi


while (true)
do

if ps aux | grep "$appname" | grep -v grep >/dev/null ;
then
	sleep 10
else
if [[ $type == "menu" ]]
then
	echo "The application \"$appname\" crashed at " `date`
	echo "The application \"$appname\" crashed at " `date` >> $errorfile

else
	echo "The application \"$appname\" crashed at " `date` >> $errorfile

fi

if [[ $count == "4" ]]
then
echo "ERROR: $appname has crashed 5 times in this session."
echo "ERROR: $appname has now been stopped"
echo "ERROR: $appname's log is now being emailed..."





exit
fi
	

if [[ -f $appname ]]
then
if [[ $type == "menu" ]]
then
	echo "Restarting \"$appname\" at " `date`
fi
	echo "Restarting \"$appname\" at " `date` >> $errorfile
	bash $appname 2>/dev/null
if [[ "$?" -eq "0" ]]
then
 	echo >/dev/null
else
	$appname 2>/dev/null
if [[ "$?" -eq "0" ]]
then
 	echo >/dev/null
else
	echo "\"$appname\" failed to execute successfully at " `date` >> $errorfile
fi
fi
else
	echo "ERROR: Application \"$appname\" not found!   " >> $errorfile
fi


((count++))


fi


done
}


function menu {
type="menu"
echo -n "Name of the error log file: "
read errorlog
echo -n "Name of application you would like to run: "
read name
if [[ -f $name ]]
then
	bash $name 2>/dev/null
if [[ "$?" -eq "0" ]]
then
	backgroundchecker $errorlog $name $type
else
	$name 2>/dev/null
if [[ "$?" -eq "0" ]]
then
	backgroundchecker $errorlog $name $type
else
	echo "$name failed to execute successfully at " `date`
	echo "$name failed to execute successfully at " `date` >> $errorlog
fi
fi
else
	echo "ERROR: Application \"$name\" not found!"
	echo "ERROR: Application \"$name\" not found!" >> $errorlog
fi
}





function cli {
type="cli"
if [[ -f $2 ]]
then
	bash $2 2>/dev/null
if [[ "$?" -eq "0" ]]
then
	backgroundchecker $1 $2 $type
else
	$2 2>/dev/null
if [[ "$?" -eq "0" ]]
then
	backgroundchecker $1 $2 $type
else
	echo "$2 failed to execute successfully at " `date` >> $1
fi
fi
else
	echo "ERROR: Application \"$2\" not found!   " >> $1
fi
}





function interactive {
if [[ $2 == "" &&  $1 == "" ]]
then
	menu
else
if [[ $2 == "" || $1 == "--help" ]]
then
	echo "Syntax: runapp log_location application"
else
	cli $1 $2
fi
fi
}


interactive $1 $2

Last edited by sleepykangaroo; 05-12-2008 at 10:50 PM.
 
Old 05-13-2008, 12:40 AM   #2
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 573

Rep: Reputation: 61
Sorry to digress immediately from a solution to an alternative, but have you looked at "Monit"? Why reinvent the wheel?
 
Old 05-13-2008, 12:51 AM   #3
sleepykangaroo
LQ Newbie
 
Registered: May 2008
Posts: 5

Original Poster
Rep: Reputation: 0
umm ok. not really my question but thanks?
Its a personal Project that i will customize in the future.
 
Old 05-13-2008, 12:54 AM   #4
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 573

Rep: Reputation: 61
Try adding "set -x" to line two of your script, and see if the extra output helps you debug the problem.

Perhaps post the results for extra eyes....
 
Old 05-13-2008, 01:21 AM   #5
sleepykangaroo
LQ Newbie
 
Registered: May 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks, if you think of anything else to help that would be great
just not at my linux box atm so i'll have to wait a few hours to see.
 
  


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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
Bash development - move chmod options to bash jhwilliams Programming 9 06-26-2007 12:13 PM
Bash Sleep vs crontab and bash serial port nutthick Programming 4 06-01-2006 02:42 AM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 05:20 PM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM

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

All times are GMT -5. The time now is 04:12 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