0

I have a php script that executes a function and creates an xml file with results. I want this script runs only once daily and i'm using wordpress cron to do this. Problem is this script always runs on all pages load and this is bad because it takes more than 30 sec to complete. How can i stop it always runs and make it works only with wp-cron ? I read about cronjob but my host hasn't this feature. This is the code i use at top of my php file :

 add_action( 'my_cron_hook', 'myfunction' );

if( !wp_next_scheduled( 'my_cron_hook' ) ) {
 wp_schedule_event( time(), 'daily', 'my_cron_hook' );
}

register_deactivation_hook( __FILE__, 'my_deactivate' );

function my_deactivate() {
 $timestamp = wp_next_scheduled( 'my_cron_hook' );
 wp_unschedule_event($timestamp, 'my_cron_hook' );
}

I want script runs daily and always in background , it should not affect user navigation. Thanks.

Edit.

This is all code script :

 add_action( 'cO_cron_hook', 'remoteHeader' );

 if( !wp_next_scheduled( 'cO_cron_hook' ) ) {
 wp_schedule_event( time() + 24 * 60 * 60, 'daily', 'cO_cron_hook' );
 }

 register_deactivation_hook( __FILE__, 'bl_deactivate' );

function bl_deactivate() {
$timestamp = wp_next_scheduled( 'cO_cron_hook' );
wp_unschedule_event($timestamp, 'cO_cron_hook' );
}

set_time_limit(300);

function remoteHeader($url){
$online = 'online';
$offline = 'offline';
$agent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL,$url );
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch,CURLOPT_VERBOSE,false);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch,CURLOPT_SSLVERSION,3);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode >= 200 && $httpcode < 400) return $online;
else return $httpcode;
}

/* create a dom document with encoding utf8 */
$domtree = new DOMDocument('1.0', 'UTF-8');

/* create the root element of the xml tree */
$xmlRoot = $domtree->createElement("xml");
/* append it to the document created */
$xmlRoot = $domtree->appendChild($xmlRoot);

$currentSite = $domtree->createElement("site");
$currentSite = $xmlRoot->appendChild($currentSite);

/* you should enclose the following two lines in a cicle */
$site = remoteHeader("http://google.com/");
$currentTrack->appendChild($domtree->createElement('google',$site));

$site2 = remoteHeader("http://wordpress.com/");
$currentTrack->appendChild($domtree->createElement('wordpress',$site2));

...

header("Content-Type: text/plain");
/* get the xml printed */
echo $domtree->save('mytest.xml')
1

WP_Cron isn't a real cron scheduler.

What it does is schedule events, mark when they should be run, and wait for page traffic. When the site is loaded, it checks to see if any of these tasks need to be run (i.e. the time they were scheduled for has passed) and runs them. So you see, WordPress' cron system is entirely dependent on your site receiving traffic.

However, it doesn't run the scheduled code in the same process. WordPress posts back to itself (i.e. loading another copy of WordPress for a separate request) and runs the code there. If you're seeing a delay in your page load due to this script running, then it means you have something else going on.

Also, your call to wp_schedule_event is wrong. The first parameter should be the time you want the task to run ... not "now" if you only want things to run once per day. Rewrite this to:

wp_schedule_event( time() + 24 * 60 * 60, 'daily', 'my_cron_hook' );

Finally, if this PHP script can run outside of WordPress, you'd be much better suited to schedule a regular event in your system's crontab. Your system administrator or host can help with that directly.

  • Thanks for suggestions. I added your code but do same. Problem is , this script (plugin) runs on every page. Maybe is because wordpress add it to its functions? My host not let me make cron with a timeout more than 5 sec (script need near 40). Is there some way to make this with only pkp ? I edited my question and added all code. Thank you . – Mr.M Jul 17 '13 at 18:45
  • You might want to consider a new host then. 5 second time out is pretty weak. – Vigs Jul 17 '13 at 20:15
0

If your web host won't let you have access to cron jobs, you can try one of the free external cron job services (I used one of these but now forgot which one)

https://www.google.com/search?q=web+cron+free

... if your script has a public URL. I added a hashed code to my script to make sure no one could just call the script directly w/o the key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.