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';
}
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';
}
Share
Improve this question
asked Jul 23, 2015 at 12:04
chapchap
3581 gold badge4 silver badges19 bronze badges
1
- 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 Commented Jul 23, 2015 at 13:49
2 Answers
Reset to default 1You'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/MikeNGarrett/e20d77ca8ba4ae62adf5
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
.
- HowTo: Add Jobs To cron Under Linux or UNIX?
- Cron Jobs calling a PHP script with variables
- Crontab Guru