1

I know that WordPress has a faux cron that is called like so when a user visits the page.

register_activation_hook(__FILE__, 'my_activation');
add_action('my_hourly_event', 'do_this_hourly');

function my_activation() {
    wp_schedule_event(time(), 'hourly', 'my_hourly_event');
}

function do_this_hourly() {
    // do something every hour
}

I want to use linux cron like so:

*/5 * * * * * /usr/local/bin/php/ /Path/to/file

The problem is that if that file is like this:

<?php 

function do_something() {

  echo 'Hello World';

}

do_something(); 

Do something function is called every time I reload the page. What I need is a way to specify with linux cron so that do_something() is only called when it is time to run cron. So the file should just have the function definition of do_something() it shouldn't call the function directly.

<?php 

 function do_something() {

   echo 'Hello World';

 }
  • why not just get the time of day stored in a variable and then use that on a switch statement where if the time is xx then it'll do it? – Alexander Jul 23 '15 at 13:49
1

You're missing one key component.

Add this to your wp-config.php: define( 'DISABLE_WP_CRON', true );

Here are some other helpful constants for your wp-config.php: https://gist.github.com/MikeNGarrett/e20d77ca8ba4ae62adf5

1

Your cron jobs only run when you tell them to.

  • 1: Minute (0-59)
  • 2: Hours (0-23)
  • 3: Day (0-31)
  • 4: Month (0-12 [12 == December])
  • 5: Day of the week(0-7 [7 or 0 == sunday])
  • /path/to/command - Script or command name to schedule

* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

But it could be possible to pass the function name to your PHP like

1 2 3 4 5 /path/to/command arg1 arg2

Then, in your PHP file, only run the task if the argument was passed using $_SERVER or $_GET.

  • 1
    Did this help at all? Were you able to solve your problem? – jgraup Dec 10 '15 at 22:11
  • Can you please give me an example of the Script File? – JPashs May 28 at 10:24

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.