How To Create Cron Job Using PHP
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");
?>
cron
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"
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]
answered
Code Lver
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.
3.
Full PHP class with all necessary methods for authentication, editing and
deleting crontab entries.
answered
Nikolay Ivanov
1,141
add a comment
up
vote0do
wn vote
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
select an editor (sometime it asks for the editor) and this to run for every minute
*
answered
Rafter
877
add a comment
up vote0down
vote
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)
{
We have used PHP Options to store the last time in seconds when task was
executed.
Here is the code for cron.php
<?php
//allow the script to run forever. this is optional depending on how heavy
is the task
set_time_limit(0);
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
Pradeep
11 days ago
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