PDA

View Full Version : 24/7 script


gridman
01-09-2007, 06:19 PM
I need my cgi script to run 24/7. Are we allowed to have our scripts run 24/7? Wouldn't the script end whenever the server is rebooted? Then I would have to always restart the script. Is there a way to get my script to run 24/7 and have it automatically restarted if for any reason it stopped running such as if my website was down or the server gets rebooted etc.

McCoy
01-09-2007, 07:21 PM
Use cron, you can configure it via CPanel. But don't make it to execute every second or your account can be shutted down due to CPU overuse.

I'm fairly sure, whatever it may be, that executing it every hour would be enough.

shadmego
01-10-2007, 06:20 AM
I also think that Hostmonster has a time limit for any one script to be running. I have 5 minutes in my head for some reason, but they will be able to tell you for sure how long a script is allowed to run.

Like McCoy said, if you use Cron, you won't have to worry about server reboots. Your script can be set up to run once every hour on the hour.

What is it your script is doing?

gridman
01-11-2007, 05:45 PM
I thought I set up cron but it's no working and I don;t know why. I can manually run my cgi-script and it works fine without producing any errors. My cron is not automatically running it no matter what frequency I set it to run..
Any help?

shadmego
01-11-2007, 08:44 PM
sometimes you have to put the path to the interpreter and then the path to the script you want to run. So your cron job would look like this:

/15 * * * * /path/to/perl /home/account/public_html/cgi-bin/path/to/script

If you don't want any output from the command, add


> /dev/null


to the end of the line. This will redirect output to the "bit bucket"

Post back if you are still having problems.

gridman
01-11-2007, 10:50 PM
It's unclear to me as exactly how to set up this thing to work.

Should I be making special file?

I thought that setting it up ONLY required setting the times at the crontab jobs from CPANEL!

McCoy
01-12-2007, 01:13 AM
You need to set up the times and of course WHAT to execute! :P

What shadmego said is that in cron most of the times you need to specify the interpreter if what you execute is not a "real" executable but some script. In this case, if it's a perl script and it's not working, you need to specify the interpreter and then pass the script to the interpreter.

It's like if you write "edit file.txt" in ms-dos: "edit" would be the program (in the case of perl, the interpreter) and "file.txt" the file to be taken by the program.

So in cron, you need to put "perl yourscript.pl", BUT, if perl is not in the path, you must specify the FULL PATH to perl which usually is /usr/bin/perl, and in any case you will most surelly need to specify the full path to your script because we don't really know where is cron being executed in the system!

So basically, you will have to put in the "executable to execute" part of cron configuration in CPanel something like this:

/usr/bin/perl /home/yourusername/public_html/cgi-bin/yourscript.pl


That is the case only if by writing /home/yourusername/public_html/cgi-bin/yourscript.pl doesn't work directly!

gridman
01-12-2007, 01:57 AM
Thanks all for helping. It seems that my scripting is RUNNING after all. But it's ot working.

I'm receiving emails every time cron runs the script. But the odd thing is, I don't see any change taking place. My script is suppose to rewrite a few files on my website.

now if I call the script from my browser addres bar (ie. www.mydomein.com/myscript.cgi) the script works perfectly. i can clearly see the changes made to my files.

But I keep getting the emails that indicates that cron is running the script but I don't see any changes at all to my files.
I've flushed my cache on my computer. The files are not changed.
I can't figure it out. The script is running but it's not doing anything.

shadmego
01-12-2007, 06:27 AM
It sounds like you have a pemissions problem. Try changing your permissions on the script you want to run to 777 for now, then see if the cron will run the script properly. If so, try changing the script to permissions of 755. If you need help doing this, let us know.

I've also heard of scripts not liking to be called directly be the interpreter. I don't have a reason for this, but there is a way around it if you use the wget command.

So using the explanaition that McCoy gave, type this in your crontab:

/15 * * * * wget -q -O /dev/null http://www.yourdomain.com/cgi-bin/path/to/script.cgi


wget is a command known as Web Get. That means you can grab files through the command line and it acts like it's using a browser.

The arguement -q is for quiet output. It basically turns off output for wget.
The -O /dev/null is saying, put write all output to the file /dev/null, or whatever you specify. It's a bit redundant, but makes sure not to clutter your site with useless data.

Let us know how that works.


~regards,
Shadmego

gridman
01-13-2007, 10:12 PM
Thank you very much for that last suggestion. I had spent the last 48 hours trying every possible thing I could thing of in order to have cron run the script with the right permissions and nothing would work.

I modified your last suggestion:

instead of giving wget to cron I just used wget in a script which then used wget to call the actual script.

here's how it finally works

1. cron runs script1 every 30 minutes

2. script1 then gives the actual url for script2 to wget

3. wget makes an actual call for script2 ... htttp://www.mydomain.com/cgi-bin/script2.cgi

4. script2 makes the changes to "index.html" file

5. !!! my index.html gets update every 30 minutes !!!


I know that this is a complicated way of getting the job done, but it has it's advantages.
Firstly, I hate scripts on the front page of a website. It makes things slow for both the user and the server. The server has to execute the script every time the file is requested and the user has to wait a little bit longer for the script to execute. With the way I have it now, the script is executed only 1 time every 1/2 hour, it then writes a plain html file. Things happen faster and I use less CPU time.

Thanks again all for your help!:)