Dynadot โ€” .com Transfer

Restart httpd on fail

Namecheap AuctionsNamecheap Auctions
Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
Watch

Crusader

Established Member
Impact
6
I'm using this script (I think I got it off of WHT) to restart httpd whenever a file failed to download on my server.

Code:
#!/bin/bash

FILE=http://www.domain.tld/test.file

rm -f test.file
wget -q $FILE -O test.file

if [ ! -f test.file ]
then
       /etc/init.d/httpd restart
       echo 'ALERT - HTTPD restarted on:' `date` | mail -s "Alert: HTTPD Restarted" [email protected]
fi

exit $?

I have it on crontab

Code:
*/5 * * * * /bin/nfup > /dev/null 2>&1

but it didn't seem to restart httpd, I got no e-mail and our site was down for several hours.

Is there anything wrong with the script?

Thanks
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable Domains โ€” AI StorefrontUnstoppable Domains โ€” AI Storefront
I'm guessing you don't have write permissions for test.file, wherever your script is trying to dump it. Add an absolute path to a location where you have write permissions.

Code:
rm -f [b]/path/you/can/write/to/[/b]test.file
wget -q $FILE -O [b]/path/you/can/write/to/[/b]test.file

if [ ! -f [b]/path/you/can/write/to/[/b]test.file ]

You might also need to set umask at the start of the script to make sure you can read/write the file as well, but try the above first.
 
0
•••
It is also possible that the user running the script does not have permissions to restart the service (you could see what user is running the process by doing a whoami command within the script).

Out of interest why are you using a php script, why not just use a bash script?
 
0
•••
The cron was running as ROOT and i've seen the file show up before. Someone on WHT suggested I use this:

Code:
#!/bin/sh
FILE=http://www.domain.tld/test.file
wget -q $FILE

if [ ! -f test.file ]

then
/etc/init.d/httpd restart
echo 'ALERT - HTTPD restarted on:' `date` | mail -s "Alert: HTTPD Restarted" [email protected]
fi

rm -rf test.file*

Apparently there's something wrong with the -O

edit: isn't this running on bash?
 
0
•••
that looks like it will be a better opting than using a php script.
 
0
•••
If it's still not working, try sending the output to a file in some writeable directory (instead of /dev/null) so you can see any errors it spits out:

*/5 * * * * /bin/nfup > /tmp/somefilename 2>&1

If it's a cron issue, most cron problems are usually either paths, environments or permissions. Cron runs with a limited environment, not the user's standard login environment so things need to be spelled out in your script.

Once you get this running, I hope next thing on the agenda is to try to find out why httpd is crashing ? Sounds like you have much worse problems than getting this script to run!
 
Last edited:
0
•••
Talked it over on a few forums and came up with this:

Code:
#!/bin/bash
tmp_file="/home/user/test_file"
wget -t 1 -T8 -O $tmp_file http://www.domain.tld/file.html >> /dev/null 2>&1
test=`grep OK $tmp_file`
if [ "$test" == "" ]; then
        #echo "File download failed, assuming offline."
        /etc/init.d/httpd restart
        echo 'ALERT - HTTPD restarted on:' `date` | mail -s "Alert: HTTPD Restarted" [email protected]
elif [ "$test" == "<html>OK</html>" ]; then
        echo "HTTPD seems to be OK" >> /dev/null 2>&1
else
        #echo "Unknown error..."
        echo 'ALERT - HTTPD had an unkown error on:' `date` | mail -s "Alert: HTTPD Error" [email protected]
fi
rm -rf $tmp_file

Apparently WGET will download something even if it isn't there so you should check what's in the file first. I don't know if this'll work though but I'm hoping.

Note: file.html should contain
Code:
<html>OK</html>
 
Last edited:
0
•••
Crusader said:
Talked it over on a few forums and came up with this:

Code:
#!/bin/bash
tmp_file="/home/user/test_file"
wget -t 1 -T8 -O $tmp_file http://www.domain.tld/file.html >> /dev/null 2>&1
test=`grep OK $tmp_file`
if [ "$test" == "" ]; then
        #echo "File download failed, assuming offline."
        /etc/init.d/httpd restart
        echo 'ALERT - HTTPD restarted on:' `date` | mail -s "Alert: HTTPD Restarted" [email protected]
elif [ "$test" == "<html>OK</html>" ]; then
        echo "HTTPD seems to be OK" >> /dev/null 2>&1
else
        #echo "Unknown error..."
        echo 'ALERT - HTTPD had an unkown error on:' `date` | mail -s "Alert: HTTPD Error" [email protected]
fi
rm -rf $tmp_file

Apparently WGET will download something even if it isn't there so you should check what's in the file first. I don't know if this'll work though but I'm hoping.

Note: file.html should contain
Code:
<html>OK</html>
http://www.rfxnetworks.com/sim.php
 
0
•••
Dynadot โ€” .com TransferDynadot โ€” .com Transfer
CatchedCatched
Escrow.com
Spaceship
Domain Recover
CryptoExchange.com
Catchy
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back