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
$command = "php ".CRON_PATH.php ";
if(substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $command, "r"));
}else{
shell_exec($command ." > /dev/null &");
}
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
In short:
Although the syntax of scheduling a new job may seem daunting at first glance, it's actually relatively simple to understand once you break it down. A cron job will always have five columns each of which represent a chronological 'operator' followed by the full path and command to execute:
* * * * * home/path/to/command/the_command.sh
Each of the chronological columns has a specific relevance to the schedule of the task. They are as follows:
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.
So, for example, if one wanted to schedule a task for 12am on the first day of every month it would look something like this:
0 0 1 * * home/path/to/command/the_command.sh
If we wanted to schedule a task to run every Saturday at 8:30am we'd write it as follows:
30 8 * * 6 home/path/to/command/the_command.sh
There are also a number of operators which can be used to customize the schedule even further:
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
Visit the link for the full article, it explains:
There is a simple way to solve this: you can execute php file by cron every 1 minute, and inside php executable file make "if" statement to execute when time "now" like this
<?/** 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 */
}
First open your SSH server with username and password and change to the default root user(User with all permissions) then follow the steps below,
crontab -l
now you will see the list of
all cronjobs.crontab -e
a file with all cron jobs will be
opened.min hr
dayofmonth month dayofweek pathtocronjobfile
and save the file.crontab: installing new
crontab
now again check the list of cronjobs your cron job will be
listed there.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.
Create a cronjob like this to work on every minute
* * * * * /usr/bin/php path/to/cron.php &> /dev/null