Run PHP function every month - php

I need to run a PHP function every month that checks Tor Exit Nodes, I have the function, but I'm not sure how to run it every month.
Here's the function:
$list = "http://some-tor-list.com/torlist/";
$tor = file_get_contents($list);

You have two options, using a crontab (recommended) or a script that runs constantly (not really recommended).
Using crontab you can run a PHP script the first day of every month using:
0 0 1 * * php /srv/http/tor.php
You can edit your crontab using crontab -e
or you could use the sleep function like this to run every month in PHP:
do {
$list = "http://some-tor-list.com/torlist/";
$tor = file_get_contents($list);
sleep(60 * 60 * 24 * 30);
} while(true);
You should read up about the syntax of crontab, it's extremely powerful.

What you are looking for is called a "cron job". Here is a SO question that answers how to create one:
How to create cron job using PHP?

You can use Cron to complete these
if in cpanel you'll find this easily
0 0 * * * php /home/site/cron.php
Or if you have a Dedicated server you can open cron tab
Look Here For the more crontab options

You need to create a php script with the content:
$list = "http://some-tor-list.com/torlist/";
$tor = file_get_contents($list);
And then use a cron job to run it every month.

Related

PHP - do at every midnight [duplicate]

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 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.
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:
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.
Full PHP class with all necessary methods for authentication, editing and deleting crontab entries.
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. So, for the 'executable' version - add executable permission to the file:
chmod +x /usr/local/bin/run.php
and then, add the following entry into crontab:
* * * * * /usr/local/bin/run.php &> /dev/null
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.
Better use the project Cron in combination with the Linux cronjob for this task. It allows you to configure run times in your PHP Code, support background jobs and is easy to use.
First step call a PHP Script every minute:
* * * * * /usr/local/bin/run.php &> /dev/null
Second Step use the cron/cron Package to configure run times directly in PHP.
$deprecatedStatus = new ShellJob();
$deprecatedStatus->setCommand('cd /app && /usr/local/bin/php cron/updateDeprecatedStatus.php');
$deprecatedStatus->setSchedule(new CrontabSchedule('* * * * */2'));
$displayDate = new ShellJob();
$displayDate->setCommand('cd /app && /usr/local/bin/php cron/updateDisplayDate.php');
$displayDate->setSchedule(new CrontabSchedule('* * * * */5'));
You found the details how to use in the linked repository.
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
That may depend on your web host if you are not hosting your own content. If your web host supports creating chron jobs, they may have a form for you to fill out that lets you select the frequency and input the absolute path to the file to execute. For instance, my web host (DreamHost) allows me to create custom cron jobs by typing in the absolute path to the file and selecting the frequency from a select menu. This may not be possible for your server, in which case you need to either edit the crontab directly or through your host specific method.
As Alister Bulman details above, create a PHP file to run using CLI (making sure to include #!/usr/bin/env php at the very start of the file before the <?php tag. This ensures that the shell knows which executable should be invoked when running the script.
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,
enter the command crontab -l now you will see the list of
all cronjobs.
enter crontab -e a file with all cron jobs will be
opened.
Edit the file with your cronjob schedule as min hr
dayofmonth month dayofweek pathtocronjobfile and save the file.
Now you will see a response crontab: installing new
crontab now again check the list of cronjobs your cron job will be
listed there.
Create a cronjob like this to work on every minute
* * * * * /usr/bin/php path/to/cron.php &> /dev/null
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;
}
}
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 */
}
why you not use curl?
logically, if you execute php file, you will execute that by url on your browser.
its 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);
}

Set a cron job and reaccess forever using PHP

I have a file on the server that extract some data from pages, like a crawler. Ok, now, sometimes, the execution of the script takes 5 seconds, but sometimes takes even 1 minute or 2.
Setting a cron job for accessing the file at 2, 3, or 5 minute is not comfortable for me, because I need this crawler to run as fast as possible.
So my question is:
Can I set a cron job to run, let's say, every 5 minutes and set php to re-run the script again and again and again ?
More clear:
*/5 * * * * wget -O - site.com/index.php?cron >/dev/null 2>&1
index.php?cron
function cron_action()
{
//some action here
// Call again the function
cron_action();
}
cron_action();
As I don't understand very well how does cron job react at my script, I don't know either what will happen when, on another 5 minutes, the cron job will acces the url again.
I will be in a infinity loop ?
How you would do that ? I need some advices please. I really need to set the cron job run faster and, in my opinion, the functions from php must be recalled forever.
Setup cronjob:
* * * * * php /path/to/your/file.php --arg1=value1 --arg2=value2 >> /dev/null 2>&1
Your file.php
<?php
//test your args
var_dump($argv);
$minute = date('i',time());
//run every 10 minutes
if(intval($minute)%10==0){
//run your cron job code here
cron_action();
}
else{
//do other code
}
function cron_action()
{
//do stub
}
And this is how to use cronjob with laravel https://laravel.com/docs/5.3/scheduling, you will learn from that.
Possibly you can use the crontab to run it faster, but you can have a pid-lock(sort of) so that each time the crontab is called there can only one script running.

How to have a cron job run a specific script every day to Query a new quote [duplicate]

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 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.
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:
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.
Full PHP class with all necessary methods for authentication, editing and deleting crontab entries.
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. So, for the 'executable' version - add executable permission to the file:
chmod +x /usr/local/bin/run.php
and then, add the following entry into crontab:
* * * * * /usr/local/bin/run.php &> /dev/null
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.
Better use the project Cron in combination with the Linux cronjob for this task. It allows you to configure run times in your PHP Code, support background jobs and is easy to use.
First step call a PHP Script every minute:
* * * * * /usr/local/bin/run.php &> /dev/null
Second Step use the cron/cron Package to configure run times directly in PHP.
$deprecatedStatus = new ShellJob();
$deprecatedStatus->setCommand('cd /app && /usr/local/bin/php cron/updateDeprecatedStatus.php');
$deprecatedStatus->setSchedule(new CrontabSchedule('* * * * */2'));
$displayDate = new ShellJob();
$displayDate->setCommand('cd /app && /usr/local/bin/php cron/updateDisplayDate.php');
$displayDate->setSchedule(new CrontabSchedule('* * * * */5'));
You found the details how to use in the linked repository.
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
That may depend on your web host if you are not hosting your own content. If your web host supports creating chron jobs, they may have a form for you to fill out that lets you select the frequency and input the absolute path to the file to execute. For instance, my web host (DreamHost) allows me to create custom cron jobs by typing in the absolute path to the file and selecting the frequency from a select menu. This may not be possible for your server, in which case you need to either edit the crontab directly or through your host specific method.
As Alister Bulman details above, create a PHP file to run using CLI (making sure to include #!/usr/bin/env php at the very start of the file before the <?php tag. This ensures that the shell knows which executable should be invoked when running the script.
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,
enter the command crontab -l now you will see the list of
all cronjobs.
enter crontab -e a file with all cron jobs will be
opened.
Edit the file with your cronjob schedule as min hr
dayofmonth month dayofweek pathtocronjobfile and save the file.
Now you will see a response crontab: installing new
crontab now again check the list of cronjobs your cron job will be
listed there.
Create a cronjob like this to work on every minute
* * * * * /usr/bin/php path/to/cron.php &> /dev/null
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;
}
}
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 */
}
why you not use curl?
logically, if you execute php file, you will execute that by url on your browser.
its 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);
}

sending automatic email for email-subscription [duplicate]

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 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.
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:
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.
Full PHP class with all necessary methods for authentication, editing and deleting crontab entries.
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. So, for the 'executable' version - add executable permission to the file:
chmod +x /usr/local/bin/run.php
and then, add the following entry into crontab:
* * * * * /usr/local/bin/run.php &> /dev/null
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.
Better use the project Cron in combination with the Linux cronjob for this task. It allows you to configure run times in your PHP Code, support background jobs and is easy to use.
First step call a PHP Script every minute:
* * * * * /usr/local/bin/run.php &> /dev/null
Second Step use the cron/cron Package to configure run times directly in PHP.
$deprecatedStatus = new ShellJob();
$deprecatedStatus->setCommand('cd /app && /usr/local/bin/php cron/updateDeprecatedStatus.php');
$deprecatedStatus->setSchedule(new CrontabSchedule('* * * * */2'));
$displayDate = new ShellJob();
$displayDate->setCommand('cd /app && /usr/local/bin/php cron/updateDisplayDate.php');
$displayDate->setSchedule(new CrontabSchedule('* * * * */5'));
You found the details how to use in the linked repository.
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
That may depend on your web host if you are not hosting your own content. If your web host supports creating chron jobs, they may have a form for you to fill out that lets you select the frequency and input the absolute path to the file to execute. For instance, my web host (DreamHost) allows me to create custom cron jobs by typing in the absolute path to the file and selecting the frequency from a select menu. This may not be possible for your server, in which case you need to either edit the crontab directly or through your host specific method.
As Alister Bulman details above, create a PHP file to run using CLI (making sure to include #!/usr/bin/env php at the very start of the file before the <?php tag. This ensures that the shell knows which executable should be invoked when running the script.
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,
enter the command crontab -l now you will see the list of
all cronjobs.
enter crontab -e a file with all cron jobs will be
opened.
Edit the file with your cronjob schedule as min hr
dayofmonth month dayofweek pathtocronjobfile and save the file.
Now you will see a response crontab: installing new
crontab now again check the list of cronjobs your cron job will be
listed there.
Create a cronjob like this to work on every minute
* * * * * /usr/bin/php path/to/cron.php &> /dev/null
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;
}
}
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 */
}
why you not use curl?
logically, if you execute php file, you will execute that by url on your browser.
its 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);
}

How to create cron job using PHP?

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 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.
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:
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.
Full PHP class with all necessary methods for authentication, editing and deleting crontab entries.
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. So, for the 'executable' version - add executable permission to the file:
chmod +x /usr/local/bin/run.php
and then, add the following entry into crontab:
* * * * * /usr/local/bin/run.php &> /dev/null
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.
Better use the project Cron in combination with the Linux cronjob for this task. It allows you to configure run times in your PHP Code, support background jobs and is easy to use.
First step call a PHP Script every minute:
* * * * * /usr/local/bin/run.php &> /dev/null
Second Step use the cron/cron Package to configure run times directly in PHP.
$deprecatedStatus = new ShellJob();
$deprecatedStatus->setCommand('cd /app && /usr/local/bin/php cron/updateDeprecatedStatus.php');
$deprecatedStatus->setSchedule(new CrontabSchedule('* * * * */2'));
$displayDate = new ShellJob();
$displayDate->setCommand('cd /app && /usr/local/bin/php cron/updateDisplayDate.php');
$displayDate->setSchedule(new CrontabSchedule('* * * * */5'));
You found the details how to use in the linked repository.
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
That may depend on your web host if you are not hosting your own content. If your web host supports creating chron jobs, they may have a form for you to fill out that lets you select the frequency and input the absolute path to the file to execute. For instance, my web host (DreamHost) allows me to create custom cron jobs by typing in the absolute path to the file and selecting the frequency from a select menu. This may not be possible for your server, in which case you need to either edit the crontab directly or through your host specific method.
As Alister Bulman details above, create a PHP file to run using CLI (making sure to include #!/usr/bin/env php at the very start of the file before the <?php tag. This ensures that the shell knows which executable should be invoked when running the script.
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,
enter the command crontab -l now you will see the list of
all cronjobs.
enter crontab -e a file with all cron jobs will be
opened.
Edit the file with your cronjob schedule as min hr
dayofmonth month dayofweek pathtocronjobfile and save the file.
Now you will see a response crontab: installing new
crontab now again check the list of cronjobs your cron job will be
listed there.
Create a cronjob like this to work on every minute
* * * * * /usr/bin/php path/to/cron.php &> /dev/null
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;
}
}
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 */
}
why you not use curl?
logically, if you execute php file, you will execute that by url on your browser.
its 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);
}

Categories