How to create a cron job using PHP?

I am new to cron. I don’t even know how to write it. I tried to search from the Internet, but I still do not understand it well. I want to create a cron job that will execute my code every minute. I use PHP to create it. He does not work.

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 these two files are in the same folder.

Is the code incorrect? If not, please tell me how to fix it.

+65
php cron
Sep 11 '13 at 9:27
source share
8 answers

This is the best explanation with the code in PHP I've found so far:

http://code.tutsplus.com/tutorials/managing-cron-jobs-with-php--net-19428

In short:

Although the syntax for scheduling a new task may seem complicated at first, it is actually relatively easy to understand once you add it. The cron task will always contain five columns, each of which is a chronological “statement”, followed by the full path and command to execute:

* * * * * home / path / to / command / the_command.sh

Each of the chronological columns has a special relation to the task schedule. They look like this:

 Minutes represents the minutes of a given hour, 0-59 respectively. Hours represents the hours of a given day, 0-23 respectively. Days represents the days of a given month, 1-31 respectively. Months represents the months of a given year, 1-12 respectively. Day of the Week represents the day of the week, Sunday through Saturday, numerically, as 0-6 respectively. 

enter image description here

So, for example, if you planned a task at 12am on the first day of each month, it would look something like this:

0 0 1 * * home / path / to / command / the_command.sh

If we planned to launch the task every Saturday at 8:30, we would write it as follows:

30 8 * * 6 home / path / to / command / the_command.sh

There are also a number of operators that can be used to further configure the schedule:

 Commas is used to create a comma separated list of values for any of the cron columns. Dashes is used to specify a range of values. Asterisksis used to specify 'all' or 'every' value 

Follow the link for the full article, it explains:

  • What is the cronjob format if you want to enter / edit it manually.
  • How to use PHP with SSH2 library for authentication as the user you want to edit crontab.
  • A complete PHP class with all the necessary authentication methods, editing and deleting crontab entries.
+35
Jul 08 '14 at 6:32
source share

Similarly, you are trying to run cron.php, you can run another PHP script. You will need to do this through the CLI.

 #!/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 crontab:

 * * * * * /usr/bin/php -f /usr/local/bin/run.php &> /dev/null 

If run.php script has executable permissions, it can be specified directly in crontab without the / usr / bin / php part. The 'env php' part in the script will find a suitable program to actually run the PHP code. Thus, for the "executable" version, add execute rights to the file:

 chmod +x /usr/local/bin/run.php 

and then add the following entry to crontab:

 * * * * * /usr/local/bin/run.php &> /dev/null 
+27
Sep 11 '13 at 9:37
source share

Added to Alister, you can edit crontab normally (not always) by entering crontab -e in the ssh session on the server.

Stars indicate (* means each of these units):

 [Minute] [Hour] [Day] [Month] [Day of week (0 =sunday to 6 =saturday)] [Command] 

You can read a few more here .

+10
Sep 11 '13 at 9:47 on
source share

There is an easy way to solve this problem: you can execute the php file using cron every 1 minute, and inside the php executable make the “if” statement execute when the time “now” is similar to

 <?/** suppose we have 1 hour and 1 minute inteval 01:01 */ $interval_source = "01:01"; $time_now = strtotime( "now" ) / 60; $interval = substr($interval_source,0,2) * 60 + substr($interval_source,3,2); if( $time_now % $interval == 0){ /** do cronjob */ } 
+5
Mar 11 '16 at 8:25
source share

Type the following in linux / ubuntu terminal

  crontab -e 

select an editor (once it asks for an editor) and it starts every minute

 * * * * * /usr/bin/php path/to/cron.php &> /dev/null 
+1
Apr 09 '14 at 5:08
source share

Create a cronjob like this to work every minute

 * * * * * /usr/bin/php path/to/cron.php &> /dev/null 
0
Sep 11 '13 at 9:33
source share
 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; } } 
0
Jan 14 '15 at 6:00
source share

why don't you use curl? logically, if you are executing a php file, you will execute it at the URL in your browser. it is 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); } 
0
Nov 01 '17 at 17:25
source share



All Articles