LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-22-2011, 03:02 PM   #1
nathanpayne
LQ Newbie
 
Registered: Jul 2011
Posts: 5

Rep: Reputation: Disabled
Bash script to restart a dead script


Hi,

I am a newbie at server setup and bash scripts so bear with me...

I have a social web application built with php and I have a php script which should run in the back ground and not stop (although it does from time to time).

I am wanting to write a bash script which will fire every minute to check if my php scrip is running, and if its not running then I need it to start it again.

I have the following but it doesn't work, it just keeps restarting it.

Code:
TEST = `pidof -x php /var/www/html/app/index.php | wc -w`

if [ $( pidof -x php /var/www/html/app/index.php | wc -w ) -eq 0 ];
then
  	echo "Restarted the Streaming...  $TEST" 1>> /home/ec2-user/bin/tmp/process.pid
	php /var/www/html/app/index.php
fi
Does anyone know where I am going wrong, or if there is a better way to monitor the script.
 
Old 07-22-2011, 03:23 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
In bash there are no spaces either side of the = assignment operator so you need TEST=`pidof -x php /var/www/html/app/index.php | wc -w`
 
Old 07-22-2011, 03:29 PM   #3
nathanpayne
LQ Newbie
 
Registered: Jul 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
thanks for that... In my output file I keep getting:
Quote:
Restarted the Streaming... 0
Restarted the Streaming... 0
Restarted the Streaming... 0
So looks like that hasn't helped, except from giving me the 0 in my output
 
Old 07-22-2011, 03:31 PM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,689

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Well I am not so sure as to where it might be going wrong, but I have obvious questions:

1. Why put the output of a command into a variable and then not use it, but rather call the whole command again??
Code:
TEST = `pidof -x php /var/www/html/app/index.php | wc -w`

if [ $( pidof -x php /var/www/html/app/index.php | wc -w ) -eq 0 ];

# the if can just be

if [ $TEST -eq 0 ];
2. Are you sure this is the correct command to test? ie when the script is running do you actually get a not zero value from your command
 
Old 07-22-2011, 03:37 PM   #5
nathanpayne
LQ Newbie
 
Registered: Jul 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
thanks for the reply

1. It was a sheer test just to see if I was able to get the value of TEST into the output file, which I do... When the php script is running it returns 1 in putty but when I am running it through bash it puts a 0 in the output file.

2. Answered in 1

Quote:
Originally Posted by grail View Post
Well I am not so sure as to where it might be going wrong, but I have obvious questions:

1. Why put the output of a command into a variable and then not use it, but rather call the whole command again??
Code:
TEST = `pidof -x php /var/www/html/app/index.php | wc -w`

if [ $( pidof -x php /var/www/html/app/index.php | wc -w ) -eq 0 ];

# the if can just be

if [ $TEST -eq 0 ];
2. Are you sure this is the correct command to test? ie when the script is running do you actually get a not zero value from your command
 
Old 07-22-2011, 03:46 PM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,689

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
So just so I have it straight, you connect via putty and run the command and it returns 1 but when connected via putty and you execute a bash script with
the same command you get zero?
 
Old 07-22-2011, 03:52 PM   #7
nathanpayne
LQ Newbie
 
Registered: Jul 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Sorry....

When I run the comman through putty I get 1
When I run the script through putty I get 1
When a crontab runs the script every 1 min it gets 0 - even though I know the scrip is running.
 
Old 07-22-2011, 04:06 PM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,689

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Ahhh ... now we are getting somewhere. Always important to paint an entire picture. So I think it is an issue with who the crontab is running as. ie that person
does not have the necessary permissions.
 
Old 07-22-2011, 04:17 PM   #9
nathanpayne
LQ Newbie
 
Registered: Jul 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
thanks for the help.

Do you know how I find out what user the crontab will be running the script as?
 
Old 07-22-2011, 11:28 PM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Quote:
Originally Posted by nathanpayne View Post
Do you know how I find out what user the crontab will be running the script as?
You could use the id command in the script.

But the issue may not be with the user; it may be with the environment set up by cron. cron sets up an enviroment which is restricted when compared with the logon environment. The usual gotcha is that PATH is basic. The PATH workaround is either for the script to set the PATH that it needs (always good defensive programming practice) or to give full paths to all commands used, for example /bin/wc for wc (assuming that is the full path of wc on the system).

If that does not fix the problem you can try changing the shebang line to make the shell set up the same environment as if it was a logon shell. For bash:
Code:
#! /bin/bash -l
That's a letter l, for logon.

If that works it is not robust to leave it that way; if anybody changes the bash initialisation files so they prompt for user input the script will fail when run by cron. If that works, it is a good test to show that something of the logon initialisation process is required and the analysis of what can begin.

Last edited by catkin; 07-22-2011 at 11:29 PM. Reason: added "when run by cron"
 
  


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
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 09:56 AM
make a bash script start automaticly when system restart sezam2 Programming 1 04-04-2009 09:27 PM
Bash script to check for dead process MaffooClock Other *NIX 12 05-18-2007 06:14 AM
Restart Service Bash Script XaViaR Linux - General 4 01-05-2006 03:10 AM
bash script for network restart munkeh Programming 8 11-10-2003 06:00 PM


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