Free Web Hosting Forum
Go Back   Free Web Hosting Forum > Website Building > Web Programming
Reload this Page running server-side php in background ?
Reply
 
Thread Tools Display Modes
(#1 (permalink))
Old
Junior Member
Xperia is on a distinguished road
 
Posts: 4
Join Date: May 2012
Default running server-side php in background ? - 05-16-2012, 02:25 PM

Hi everyone, I am a new at php and i have managed to write a php file which downloads data from the internet and update an existing XML file while logging this update in a txt file.

I tried using set_time_limit() and ignore_user_abort() and exec() and ini_set('max_execution_time', ...) but am unable to keep the script running on server in background and updating the xml file. ideally i would like it to update the XML file every half hour. Any ideas?

Any ideas or comments are welcome. Many thanks!
Reply With Quote
Sponsored Links
(#2 (permalink))
Old
Senior Member
grace1004 is on a distinguished road
 
Posts: 735
Join Date: Dec 2010
Default 05-17-2012, 01:42 AM

It might be possible by setting up a cron job. Go to your control panel - >> Advanced - click 'Cron Jobs' icon,
and fill out 'Add new Cron Job' form. For example, to execute 'update.php' file in public_html directory every
30 minutes, please set up as follows:

php -f /home/a323xxxx/ ----- public_html/update.php

select 30 in the Minute(s) select box, and every hour(*) in the Hour(s) select box. For Day(s), Month(s) and
Weekday(s), select default setting (*) - click 'Add' button.

Then, the cron job setting will be displayed in 'List of current cron jobs':

*/30 * * * * ---- php -f /home/a323xxxx/public_html/update.php

Please make sure that the permission of update.php file should be set to 777 so that it can write updated
text. It might be good to make a directory with 777 permission and put the php file in this directory, as the
permission of public_html directory should not be set to 777. Also, the path for included files in update.php
should be expressed in full path something like: include "/home/a323xxxx/public_html/db_conn.php";

From previous posts in this Forum, I noticed that cron jobs calling external data from internet, sometimes,
did not work. However, at least, you can try.

Last edited by grace1004; 05-17-2012 at 10:03 PM.
Reply With Quote
(#3 (permalink))
Old
Junior Member
Cybrax is on a distinguished road
 
Posts: 12
Join Date: Jul 2010
Default 05-20-2012, 09:56 PM

If your content grabbing script is taking longer than a minute to run then I am afraid it may also need a rethink & recoding. Either going after less data each time it runs or processing the retrieved data more efficiently after it arrives at the 000 server (or better still, before it arrives ). It all really depends upon how much data you are trying to move about and how much of that is what you actually want.

Remember the most a single CRON job can be run here is every fifteen minutes.

Note: admin here will not be happy if you try running multiple identical cron jobs all calling the same script at 15min intervals. Using an external Cron Service provider will also get you on the naughty list.


set_time_limit() is a feature normally reserved for paid hosting and even then it should only be used as a last resort, here at 000 it has been globally set to help prevent abuse of the server resources.
ini_set('max_execution_time' and ignore_user_abort() and exec() are also not available on a free hosting account and you would probably need either a VPS or dedicated server to play around with them.
Reply With Quote
(#4 (permalink))
Old
Junior Member
Xperia is on a distinguished road
 
Posts: 4
Join Date: May 2012
Default 05-21-2012, 02:07 PM

Hiya! thanks for replying!

I tried setting up a cron job but it is not executing the script at all. my update.php script contains the following and takes no more than 20 seconds to be executed. Any idea on how that can be executed on the server at regular intervals?

Quote:
<?php


function doupdatef(){
$handlefreq = fopen("http://www.*****************", "r");
$filefreq = 'rollingf******.xml';
file_put_contents($filefreq,$handlefreq);

}

function doupdated(){
$handledmd = fopen("http://www.*****************", "r");
$filedmd = 'rollingd******.xml';
file_put_contents($filedmd,$handledmd);

}




$outfile = 'DeleteMeToStopUpdate.txt'; // output file name
if ( file_exists($outfile) ): // if file DOES exist...
ini_set('max_execution_time', 40);
doupdatef();
doupdated();
$fp = fopen('updatehistory.txt', 'a');
$inputstring = "Last updated: " . (date("H:i:s")). " at timezone " .(date("T")). " USA - ". (date("l dS F Y")) . "\n";
fwrite($fp, $inputstring);
fclose($fp);



endif;


?>
Reply With Quote
(#5 (permalink))
Old
Junior Member
Cybrax is on a distinguished road
 
Posts: 12
Join Date: Jul 2010
Default 05-22-2012, 09:03 AM

Not entirely sure but if the cron job has been set up correctly as per Grace1004's instructions then it could well be a problem with the fopen command not being allowed to access the web at large. It's a security permissions for shared web hosting thing I think because fopen is capable of moving files over FTP connections, the admin known as Bad Karma should be able to clarify this. Try using an alternative commands like file_get_contents, cURL or simplexml_load_file to fetch the data instead.
Reply With Quote
(#6 (permalink))
Old
Junior Member
Xperia is on a distinguished road
 
Posts: 4
Join Date: May 2012
Default 05-22-2012, 11:22 AM

the update.php works fine when called thru url. the data is saved and logged too. Thanks for ur reply.
Reply With Quote
(#7 (permalink))
Old
Junior Member
Xperia is on a distinguished road
 
Posts: 4
Join Date: May 2012
Default 05-22-2012, 01:24 PM

Solved. cron job was set up correctly. the problem was the path of the files used, for example:

Quote:
$outfile = 'DeleteMeToStopUpdate.txt'; // output file name

was replaced with:

$outfile = '/home/a7******/public_html/data/DeleteMeToStopUpdate.txt'; // output file name
Reply With Quote
Reply

Tags
html, php

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.5.2
vBulletin Skin developed by: vBStyles.com