How to create cron job using PHP?
up
vote18d
own vote
10
favorite
I'm new to using cron job. I don't even 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
will execute my code every minute. I'm using PHP to create it. It is not working.
Example
run.php (Code that will be executed every minute)
<?php
echo "This code will run every minute";
?>
cron.php
<?php
$path = dirname(__FILE__);
$cron = $path . "/run.php";
echo exec("***** php -q ".$cron." &> /dev/null");
?>
Suppose that these two files are in the same folder.
Is the code that I did wrong? If wrong, please kindly tell me how to fix it.
Thanks in advance.
php
cron
shareimprove this question
edited Sep 11 '13 at 10:34
asked
mu
user2738520
17.1k82754
137
do you have shell access on the server? Dagon Sep 11 '13 at 9:31
3 You can't just echo out *** and expect a cronjob to be created. Read up here how to create cronjobs (assuming y
on a server running linux) thesitewizard.com/general/set-cron-job.shtml tlenss Sep 11 '13 at 9:31
@Dagon: i don't know about this. I'll check it out. user2738520 Sep 11 '13 at 9:40
It is a one off event so use crontab Ed Heal Jan 26 '14 at 16:49
add a comment
6 Answers
activeoldest
votes
up
vote9do
wn vote
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.
shareimprove this answer
answered
Alister Bulman
14.5k
add a comment
up
vote5do
wn vote
Added to Alister, you can edit the crontab usually (not always the case) by
entering crontab -e in a ssh session on the server.
The stars represent (* means every of this unit):
[Minute] [Hour] [Day] [Month] [Day of week (0 =sunday to 6 =saturday)]
[Command]
You could read some more about this here.
shareimprove this answer
edited Jul 4 '14 at 6:21
answered
Code Lver
Big Ginger Nerd
108
add a comment
up
vote4do
wn vote
This is the best explanation with code in PHP I have found so far:
http://code.tutsplus.com/tutorials/managing-cron-jobs-with-php--net-19428
It explains everything:
1.
2.
What is the format of the cronjob if you want to enter/edit it manually.
How to use PHP with SSH2 library to authenticate as the user, which
crontab you are going to edit.
3.
Full PHP class with all necessary methods for authentication, editing and
deleting crontab entries.
shareimprove this answer
answered
Nikolay Ivanov
1,141
add a comment
up
vote0do
wn vote
Create a cronjob like this to work on every minute
*
/usr/bin/php path/to/cron.php &> /dev/null
shareimprove this answer
answered
rams0610
649
1
3
OP needs more help than that, where to put that line of code Dagon Sep 11 '13 at 9:36
add a comment
up
vote0do
wn vote
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
shareimprove this answer
answered
Rafter
877
add a comment
up vote0down
vote
I have written a tutorial on how to schedule asynchronous cron jobs using
php. This method doesn't require any extensions or any other kind of coding
horror.
http://qnimate.com/php-asynchronous-cron-job-scheduling-tasks/
PHP Asynchronous Cron Job
Scheduling Tasks
In this tutorial I will provide a method to run cron jobs in PHP that too
asynchronous without affecting page load time. This doesnt require to install
any PHP extension or any other kinds of coding horrors.
Logic
We need two PHP files. The first file checks if scheduled time has passed or just
occurred for a cron job. If its time to run the task then it starts a new
asynchronous process to run the second PHP which is actually the cron callback.
Complete Code
Files are named: cron.php and schedule.php. cron.php contains the callback
code and schedule.php should be runned every time user makes a request to
the web server.
Here is the code for schedule.php
<?php
if(get_option("cron_job_1") != null)
{
//run task every 1 hour.
if(time() - get_option("cron_job_1") >= 3600)
{
$ch = curl_init();
//send a request to the cron.php file so that its started in a
new process asynchronous to the current process.
curl_setopt($ch, CURLOPT_URL,'http://yourdomain/cron.php');
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
//close connection after 1 millisecond. So that we can continue
current script exection without effect page load time.
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1);
//some versions of curl don't support CURLOPT_TIMEOUT_MS
so we have CURLOPT_TIMEOUT for them. Here we close connection after 1
second.
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_exec($ch);
curl_close($ch);
update_option("cron_job_1", time());
}
}
else
{
update_option("cron_job_1", time());
}
We have used PHP Options to store the last time in seconds when task was
executed.
Here is the code for cron.php
<?php
// Ignore user aborts
ignore_user_abort(true);
//allow the script to run forever. this is optional depending on how heavy
is the task
set_time_limit(0);
//put the task code here.
How WordPress executes Async Cron without effecting
page load timeTesting Intel XDK App on iOS Device using
Ad Hoc
Comments: 5
1.
Ramon Figueroa
3 days ago
What I have to do if I want to run this as my cron job.(see below) How I place
this in your cron.php file. Thanks for your help.
0 * * * * /usr/bin/php /path/to/moodle/auth/db/cli/sync_users.php >dev/null
Reply
2.
Pradeep
11 days ago
Sorry i just included it.its fetching data but
i changed code as
set_time_limit(60);
//put the task code here.
echo "hi pradeep";
$file = fopen(bulk_upload.csv,r);
while(! feof($file))
{
print_r(fgetcsv($file));
}
fclose($file);
?>
and my csv records as
1)pradeep varma Pradeep
[email protected] password
its displaying first record but i set time to 60 sec in between 60 sec i updated
csv file added new record
2) rakesh raju rakesh
[email protected] password
but after 60 sec its showing 1 record value only
Reply
3.
Pradeep
11 days ago
i written code as you mentioned above but its throughing
Fatal error: Call to undefined function get_option() in
D:\xampp\htdocs\check\schedule.php on line 2
please help me..
Reply
o
Narayan Prusty
11 days ago
Please read the tutorial carefully. I have mentioned to first use the code from
http://qnimate.com/storing-key-value-pairs-in-php-using-csv/
Reply
4.
Jayasri
1 month ago
Thanks a ton! This helped me a lot.