PDA

View Full Version : Server paging option



jayman228
02-18-2006, 01:27 AM
Hello,

I got a odd but weird question. I have weather software on my computer that uploads files every 5 minutes using FTP for my weather page.

My question is, is there software or anything I can put on the server that if the page (or file --lets say "index.html") doesn't update within 1 hour or so that I could have it page me via cell phone letting me know that there might be a problemn and the page isn't updating. This would really help me in case my computer locks up and im not home or gone one weekend.

I'm sure some other users have experienced this and im not sure if this would be a php thing or whatnot...but its been bugging me and needed to be pointed in the right direction.

Thanks! :-)

jdh
02-18-2006, 08:54 AM
Well, probably the most basic solution to this would be a shell script that runs as part of a Cron job.

You can setup Cron jobs in the cPanel, and you would basically just set something up that would run every hour and check the timestamp on the page that you're looking for, using a relatively simple "if" construct in the script, and then use the mail command to send an e-mail out to your cell phone's e-mail address if necessary.

The easy way to do this would probably be with shell access, but if you don't have that, you could probably still compose the script offline and upload it manually.

I am by no means a shell scripting expert, so there's probably a more elegant way to do this, but a script like the following would probably work:



#!/bin/bash

if [ "path-to-real-file" -nt "path-to-reference-file" ] then
touch "path-to-reference-file"
else
mail your-address@yourdomain.com -s a subject line <message-body-file
fi


Essentially, what this should do is create a reference file with the current date and time the first time it's run, and then use that as a timestamp reference to determine the last time the real file was checked as a comparison. The next time the script runs, it checks to see if the real file is newer than the reference file (ie, it's been updated since the last check), and if this is the case, it updates the timestamp on the reference file ("touch" command). If not, the else condition is run, which sends out an e-mail message using the text in a specific file as the body (or just use /dev/null if you don't want a message body but just a subject line).

Provided you have an e-mail address associated with your SMS service for your cell phone, you could simply provide that e-mail address in the script.

jayman228
02-18-2006, 02:31 PM
Well, probably the most basic solution to this would be a shell script that runs as part of a Cron job.

You can setup Cron jobs in the cPanel, and you would basically just set something up that would run every hour and check the timestamp on the page that you're looking for, using a relatively simple "if" construct in the script, and then use the mail command to send an e-mail out to your cell phone's e-mail address if necessary.

The easy way to do this would probably be with shell access, but if you don't have that, you could probably still compose the script offline and upload it manually.

I am by no means a shell scripting expert, so there's probably a more elegant way to do this, but a script like the following would probably work:



#!/bin/bash

if [ "path-to-real-file" -nt "path-to-reference-file" ] then
touch "path-to-reference-file"
else
mail your-address@yourdomain.com -s a subject line <message-body-file
fi


Essentially, what this should do is create a reference file with the current date and time the first time it's run, and then use that as a timestamp reference to determine the last time the real file was checked as a comparison. The next time the script runs, it checks to see if the real file is newer than the reference file (ie, it's been updated since the last check), and if this is the case, it updates the timestamp on the reference file ("touch" command). If not, the else condition is run, which sends out an e-mail message using the text in a specific file as the body (or just use /dev/null if you don't want a message body but just a subject line).

Provided you have an e-mail address associated with your SMS service for your cell phone, you could simply provide that e-mail address in the script.

JDH,

Thank You so much for your reply. I logged into my bluehost account and checked out the cronjobs and it only gives me one command line not 5 so the if statement wouldn't work there. Unless there is a shorter way I can use that line or im doing something wrong. LOL I copied and pasted that code you have above to notepad but several issues I ran into. WHat directory do I store the saved file to and also what would be the best extention to call it? (.txt., .html, etc) Does it need to be a certain extention?

If this helps I would love to have the index.html be the file it checks since thats the main website file. I really wouldn't want a body in the email just a subject line to say "index.html hasn't been updated in 60 minutes" or something along those lines.

I'd personally like to use Cronjob since its like right there and it could do the rest. Although if i have to do the file myself and ftp it onto the server thats fine, I just need to know what directory its in, when I looked before posting this message, i couldn't find where to put it :-(

Thanks!

jdh
02-18-2006, 02:47 PM
Well, you should be able to call the file anything you want, since extensions don't really mean anything in Unix. Some people use a ".sh" extension to define the file as a shell script, but that's not really necessary.

What you would do is paste the code above (modified appropriately for your situation) into a file, and then FTP that file up to your account. You can drop it in your main home directory if you like, or anywhere else.

You would then need to perform one more step to make it executable. If you have shell access (SSH), you could just log in and run "chmod 700 <filename>", which would grant read, write, and execute permission to the file. If you don't have shell access, you can do this through the cPanel file manager by selecting the file and choosing "Change Permissions" from the menu. Make sure you select the checkbox beside "Execute" in the "User" column, which will give your script execution permission for your userid.

In the cron job, you then specify the path to the script as "/home/<yourusername>/<yourscriptfilename>" (assuming you've placed the script in your main home directory).

jayman228
02-18-2006, 03:09 PM
Well, you should be able to call the file anything you want, since extensions don't really mean anything in Unix. Some people use a ".sh" extension to define the file as a shell script, but that's not really necessary.

What you would do is paste the code above (modified appropriately for your situation) into a file, and then FTP that file up to your account. You can drop it in your main home directory if you like, or anywhere else.

You would then need to perform one more step to make it executable. If you have shell access (SSH), you could just log in and run "chmod 700 <filename>", which would grant read, write, and execute permission to the file. If you don't have shell access, you can do this through the cPanel file manager by selecting the file and choosing "Change Permissions" from the menu. Make sure you select the checkbox beside "Execute" in the "User" column, which will give your script execution permission for your userid.

In the cron job, you then specify the path to the script as "/home/<yourusername>/<yourscriptfilename>" (assuming you've placed the script in your main home directory).

AHA, I made it excute on the server but no reference file is being created. Do I have this code right? I'm not familar with unix programming and i'm sure you can catch any errors before I can.

I called this file page.sh (per your suggestion)



#!/bin/bash

if ["index.html" -nt "reference.txt"] then
touch "reference.txt"
else
mail your-jayman228@verizon.net -s a subject line "Page down" /dev/null
fi


I went to con jobs and entered "/home/hobartwe/public_html/page.sh" in the command line. I told it to update every 1 minute just to beta test, if that works im then going to take my site down for 30 minutes after I change the time to 30 minutes and also modify my email address to my cell phone and see what happends!

Thanks!

jdh
02-18-2006, 08:57 PM
AHA, I made it excute on the server but no reference file is being created. Do I have this code right? I'm not familar with unix programming and i'm sure you can catch any errors before I can.
You just have a couple of minor syntax errors... Try the following, with changes in bold:


#!/bin/bash

if ["index.html" -nt "reference.txt"] then
touch reference.txt
else
mail your-jayman228@verizon.net -s a subject line "Page down" </dev/null
fi

Basically, there should be no quotes around the filename in the "touch" statement, and you need to make sure that the redirect-in symbol ("<") is in front of /dev/null, since you're basically drawing input from that into mail (which would otherwise default to requesting input from the console).

jayman228
02-18-2006, 10:21 PM
Basically, there should be no quotes around the filename in the "touch" statement, and you need to make sure that the redirect-in symbol ("<") is in front of /dev/null, since you're basically drawing input from that into mail (which would otherwise default to requesting input from the console).

JDH,

I did all that and when I checked my email I saw this in there.

/home/hobartwe/public_html/page.sh: line 5: syntax error near unexpected token `else'
/home/hobartwe/public_html/page.sh: line 5: `else'

Looks like were getting close though.

rcflyer
02-18-2006, 10:40 PM
Looks like you're just missing a semicolon on the first line:



if [ "index.html" -nt "reference.txt" ]; then
touch reference.txt
else
mail your-jayman228@verizon.net -s a subject line "Page down" </dev/null
fi

jayman228
02-19-2006, 12:56 AM
Looks like you're just missing a semicolon on the first line:



if [ "index.html" -nt "reference.txt" ]; then
touch reference.txt
else
mail your-jayman228@verizon.net -s a subject line "Page down" </dev/null
fi


RCFlyer,

I did exactly what you mentioned and this is the email I got!

/home/hobartwe/public_html/page.sh: line 5: syntax error near unexpected token `fi'
/home/hobartwe/public_html/page.sh: line 5: `fi'

Below is my code for page.sh


#!/bin/bash

if [ "index.html" -nt "reference.txt" ]; then
touch reference.txt
else
mail your-jayman228@verizon.net -s a subject line "Page down" </dev/null
fi

jdh
02-19-2006, 05:26 AM
Odd.... RCFlyer had it, it was the missing semi-colon (or you could drop the "then" to a separate line, it's the same difference):



#!/bin/bash

if [ "index.html" -nt "reference.txt" ]
then
touch reference.txt
else
mail your-jayman228@verizon.net -s a subject line "Page down" </dev/null
fi


Also, make sure there's a blank line after the "fi" statement... It's possible that might be confusing it.

One other point to note, although this won't produce a syntax error, is that the "a subject line" entry was a placehold.... you can actually just use "-s Page Down" on the mail line, and the quotes aren't required, ie:



mail your-jayman228@verizon.net -s a Page down </dev/null


Again, this wouldn't produce your syntax error, just an e-mail with a subject line that wouldn't have been what you expected.

jayman228
02-19-2006, 12:05 PM
Odd.... RCFlyer had it, it was the missing semi-colon (or you could drop the "then" to a separate line, it's the same difference):



#!/bin/bash

if [ "index.html" -nt "reference.txt" ]
then
touch reference.txt
else
mail your-jayman228@verizon.net -s a subject line "Page down" </dev/null
fi


Also, make sure there's a blank line after the "fi" statement... It's possible that might be confusing it.

One other point to note, although this won't produce a syntax error, is that the "a subject line" entry was a placehold.... you can actually just use "-s Page Down" on the mail line, and the quotes aren't required, ie:



mail your-jayman228@verizon.net -s a Page down </dev/null


Again, this wouldn't produce your syntax error, just an e-mail with a subject line that wouldn't have been what you expected.

After making the change, this is what I get in the email now......Putting the blank line at the end and changing the subject part:

/home/hobartwe/public_html/page.sh: line 7: syntax error: unexpected end of file

Code I have is


if [ "index.html" -nt "reference.txt" ]
then
touch reference.txt
else
mail your-jayman228@verizon.net -s a Page down </dev/null
fi



Any other ideas? Thanks

jdh
02-19-2006, 12:24 PM
Hmmm... I just ran your script, exactly as written, from my own Bluehost account (I copied and pasted it in), and it ran fine (in fact, you may even get some e-mails from my account).

What program are you using to create the script before you upload it? I'm wondering if there's something in there that's interfering. If you have an option in your text editor to save in a "Unix" style you should probably try that.

jayman228
02-19-2006, 12:31 PM
Wow,

I created it in notepad..Just copied and pasted in there and then went to cron jobs to make sure it ran every 1 minute (i just changed it to hourly though so i don't get slamed with emails , lol)

It might be a FTP problem. Might not be transfering as a unix format or whatnot....could that be it? I remove the file from the server and just use bluehost since I been using a FTP client instead.

hmm...well, i got a birthday party to goto...i"ll play with this somemore when I get back..if you or anyone gots anymore ideas let me know.

Thanks for all your help! :-)

jayman228
02-19-2006, 05:25 PM
Hey,

Just wanted to let you know I got it to work! :-) I removed it off the server with WS_FTP program i use to upload files (manually). Logged into bluehost to file manager and uploaded it that way, made sure it had right permissions, and checked my webmail account and it showed "Null message body; hope that's ok " Also, saw the email in my verizon inbox as well.

Since im not getting no more errors I set Cron to run every 1 hour and 5 minutes since thats the fastest upload (it only uploads 3 files vs a lot during the 15 minute per hour).

Anyways, after changing the time It was 6:50 or so I wanted to see what would happen at 7:05. I got a email from my verizon account saying "Server is down" but yet it wasn't. The file was uploaded w/out any issues. In fact I logged back on to my ws_ftp client to see what time stamp it showed and it was 18:05 at that time. I didn't however see the page.txt file its suppose to use for the touch command. I then went to notepad and created a empty page.txt file and went to my weather program and added that file to the upload list for the 1 hour and 5 minutes. So now it will be 4 files that gets uploaded to the server at the 5 minute past the hour. Now im just waiting for 8:05 to see how that works.....

Per your other messages though, wasn't the server suppose to automatically create the page.txt file? Because it didn't. Here is the actual code that works 100% for me (minus the small glitch)



if [ "index.html" -nt "page.txt" ]
then
touch page.txt
else
mail "jayman228@verizon.net" -s "Page down" </dev/null
fi


Thanks for all your help! :-)

jayman228
02-20-2006, 11:04 AM
Well, It's working, but not working right. I'm not getting no more errors through email like before , it still shows "Null message body; hope that's ok ". Thats a good sign.

I have cron run every hour and 6 minutes. My problem is the file page.txt isn't creating itself. (or I just can't find where its located - its not in the main directory were my page.sh is located.) Do I create that file and upload it? or is it automatic?

Right now my page is set to update the index.html file every 5 minutes. Sometimes it doesn't upload right away and sometimes it can be hr:05:50 seconds or something like that. So my problem im running into is the Cron job runs exactly at hr:06:01. So when cron runs it doesn't see the exact minutes/seconds and sends me the email.

To explain better.

index.html - file updates/uploads to server every 5 minutes and xx seconds (it veries) (mosley hr:05:30 or so)
Cron - set to run at every hour and 6 minutes and 01 second - it runs fast.

Cron needs to check index.html see that it has a upload of hr:05 and check page.txt that has a upload of time of hr:05 and ignore seconds since it varies.

Hopefully, i made sense..Anyway to get around that? Thanks

jdh
02-20-2006, 11:22 AM
Well, essentially, if the "page.txt" file doesn't exist, the IF statement should return TRUE (since your index.html is considered newer than a file that doesn't exist), and the touch statement should be run which will either create or update the reference file.

The only thing I can think of is that you should probably use full pathnames to the files, since I expect that index.html is in your public_html directory, whereas the script doesn't necessary run from that directory.

In fact, now that I think of it, this is likely the problem, since if the index.html file doesn't exist, the condition will evaluate to FALSE and therefore the mail will be sent. In reality, this wouldn't be a bad thing, since you would want to know if your index.html had gone away for some reason, but in this case, I suspect that it's looking for index.html in the wrong directory -- most likely your home directory, rather than your public_html directory.

With regards to your second question, the seconds still really shouldn't be an issue, since the "touch" command will place the timestamp on the file of whenever the script ran successfully. Next time the script runs, it's not using the current time of the script, but rather the last time the script ran (one hour ago, in your case)... So you're actually tracking whether or not your index.html file has been updated since the last time the script was invoked. In other words, the script isn't checking to see if index.html has an update time of :05, but rather just that it's been changed since the last time the script ran.

The sequence of events should basically be this:
You run the script for the first time at, say, 9:06 AM. The "page.txt" reference file doesn't exist, so naturally index.html is newer than a non-existent file. The condition evaluates to true, the "touch" command is run, and the page.txt file is created, with the current date and time (ie, 9:06 AM).
The script runs again at 10:06 AM. It now compares the index.html file to the "page.txt" file that was created at 9:06 AM. The page.txt file has the timestamp of 9:06 AM on it. In this case:
If the index.html file has been updated, it will naturally have a timestamp later than 9:06 AM. In this case, the statement evaluates to TRUE, and the "touch" command is run again to place the current time stamp on the "page.txt" file as a reference for the next run.
If something's broken, and the index.html file hasn't been updated, it will still have an older timestamp on it. In this case, the test evaluates to FALSE, and the else statement is run which execute the mail command to send you an e-mail message. The "page.txt" file remains untouched with the previous timestamp.
Every subsequent run of the script will evaluate the same conditions. If the page does get updated on the next run, then everything will be okay. If the page continues to not get updated, you will continue to get e-mail messages, since the index.html will still be older than the page.txt file.

jayman228
02-22-2006, 03:07 AM
JDH,

I wanted to thank you for all your help. I tested it out for a day and it worked great! My computer locked up 3 hours ago and when the page went down I got paged on my cell phone 3 times. (because I made Cron work at hr:06, hr:21, hr:36, and hr:51 - basically every 15 minutes) and after the page was computer came back online and the page started to update again, the pages stopped on my cell phone!

Again, thanks for all your help! :-)

jayman228
09-20-2009, 04:55 AM
Well, this worked back then but now it's not. (I moved, changed directories, and haven't used this since I moved. Thought it would be easy to implement but I'm having issues again.

Anyways, I changed the page.sh file to fit my new location and new email address:



if [ "index.php" -nt "page.txt" ]
then
touch page.txt
else
mail "jason@dajman.com" -s "Page down" </dev/null
fi


When crun runs, I get:



/home2/dajmanco/public_html/portalesweather/page.sh: line 7: syntax error:
unexpected end of file


I entered the blank line under fi and it still gives me this. What am I doing wrong?

Thanks,