I\'m new to using cron job. I don\'t even know how to write it. I have tried to search from internet, but I still don\'t understand it well. I want to create a cron job that
function _cron_exe($schedules) {
if ($obj->get_option('cronenabledisable') == "yes") {
// $interval = 1*20;
$interval = $obj->get_option('cronhowtime');
if ($obj->get_option('crontiming') == 'minutes') {
$interval = $interval * 60;
} else if ($obj->get_option('crontiming') == 'hours') {
$interval = $interval * 3600;
} else if ($obj->get_option('crontiming') == 'days') {
$interval = $interval * 86400;
}
$schedules['hourlys'] = array(
'interval' => $interval,
'display' => 'cronjob'
);
return $schedules;
}
}
Better use the project Cron in combination with the Linux cronjob for this task. It allows you to configure run times in your PHP Code, support background jobs and is easy to use.
First step call a PHP Script every minute:
* * * * * /usr/local/bin/run.php &> /dev/null
Second Step use the cron/cron Package to configure run times directly in PHP.
$deprecatedStatus = new ShellJob();
$deprecatedStatus->setCommand('cd /app && /usr/local/bin/php cron/updateDeprecatedStatus.php');
$deprecatedStatus->setSchedule(new CrontabSchedule('* * * * */2'));
$displayDate = new ShellJob();
$displayDate->setCommand('cd /app && /usr/local/bin/php cron/updateDisplayDate.php');
$displayDate->setSchedule(new CrontabSchedule('* * * * */5'));
You found the details how to use in the linked repository.
why you not use curl? logically, if you execute php file, you will execute that by url on your browser. its very simple if you run curl
while(true)
{
sleep(60); // sleep for 60 sec = 1 minute
$s = curl_init();
curl_setopt($s,CURLOPT_URL, $your_php_url_to_cron);
curl_exec($s);
curl_getinfo($s,CURLINFO_HTTP_CODE);
curl_close($s);
}
Type the following in the linux/ubuntu terminal
crontab -e
select an editor (sometime it asks for the editor) and this to run for every minute
* * * * * /usr/bin/php path/to/cron.php &> /dev/null
That may depend on your web host if you are not hosting your own content. If your web host supports creating chron jobs, they may have a form for you to fill out that lets you select the frequency and input the absolute path to the file to execute. For instance, my web host (DreamHost) allows me to create custom cron jobs by typing in the absolute path to the file and selecting the frequency from a select menu. This may not be possible for your server, in which case you need to either edit the crontab directly or through your host specific method.
As Alister Bulman details above, create a PHP file to run using CLI (making sure to include #!/usr/bin/env php
at the very start of the file before the <?php
tag. This ensures that the shell knows which executable should be invoked when running the script.
In the same way you are trying to run cron.php, you can run another PHP script. You will have to do so via the CLI interface though.
#!/usr/bin/env php
<?php
# This file would be say, '/usr/local/bin/run.php'
// code
echo "this was run from CRON";
Then, add an entry to the crontab:
* * * * * /usr/bin/php -f /usr/local/bin/run.php &> /dev/null
If the run.php script had executable permissions, it could be listed directly in the crontab, without the /usr/bin/php part as well. The 'env php' part, in the script, would find the appropriate program to actually run the PHP code. So, for the 'executable' version - add executable permission to the file:
chmod +x /usr/local/bin/run.php
and then, add the following entry into crontab:
* * * * * /usr/local/bin/run.php &> /dev/null