Upload
maverick
View
18
Download
5
Embed Size (px)
DESCRIPTION
Programacion
Citation preview
How to create cron job using PHP?
up vot
e18down votefavorite
10
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.
Examplerun.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
share improve this question edited Sep 11 '13 at 10:34
mu 無 17.1k82754
asked
user2738520137
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 you are 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 comment6 Answers
active oldest votes
up vot
e9down 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'// codeecho "this was run from CRON"Then, add an entry to the crontab:
* * * * * /usr/bin/php -f /usr/local/bin/run.php &> /dev/nullIf 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.
share improve this answer answered
Alister Bulman14.5k
add a comment
up vot
e5down 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.
share improve this answer edited Jul 4 '14 at 6:21
Code L ღ ver 1
answered
Big Ginger Nerd108
add a commentup vot
e4down 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-19428It explains everything:
1. What is the format of the cronjob if you want to enter/edit it manually.
2. 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.
share improve this answer answered
Nikolay Ivanov1,141
add a commentup vot
e0down vote
Create a cronjob like this to work on every minute
* * * * * /usr/bin/php path/to/cron.php &> /dev/null
share improve this answer answered
rams0610649
13 OP needs more help than that, where to put that line of code – Dagon Sep 11 '13 at 9:36
add a commentup vot
e0down 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
share improve this answer answered
Rafter877
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 doesn’t require to install any PHP extension or any other kinds of coding horrors.
LogicWe 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 CodeFiles 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 time Testing Intel XDK App on iOS Device using Ad HocComments: 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
Reply2.
Pradeep
11 days ago
Sorry i just included it.it’s fetching data but
i changed code asset_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 [email protected] password
its displaying first record but i set time to 60 sec in between 60 sec i updated csv file added new record2) rakesh raju rakesh [email protected] password
but after 60 sec its showing 1 record value only
Reply3.
Pradeep
11 days ago
i written code as you mentioned above but it’s throughing
Fatal error: Call to undefined function get_option() in D:\xampp\htdocs\check\schedule.php on line 2
please help me..
Replyo
Narayan Prusty
11 days ago
Please read the tutorial carefully. I have mentioned to first use the code fromhttp://qnimate.com/storing-key-value-pairs-in-php-using-csv/
Reply
4.
Jayasri
1 month ago
Thanks a ton! This helped me a lot.