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);
}
Related
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);
}
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);
}
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.
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);
}
My res_time.php file has a line
$r = $shell_exec("/var/www/html/new12345/cront1.sh $n");
where cront1.sh is
!/bin/bash
echo " 00 $1 * * * /var/www/html/new12345/shell_call.php >> /var/www/html/w.txt" | crontab -
when i manually enter ./cront1.sh 3 i can see an entry in crontab
However using bash within php is creating some problems..the line itself is not called..HELP!
Permissions are full to each file reffered
This isn't really the right way to go about things. Generally, you want to involve the OS-level cron as little as possible. The way most frameworks go about it is to have a single cron script, called every few minutes or so, which then decides what other tasks need doing, and does them.
So you'd set up a single entry in Crontab, something like:
*/3 * * * * /var/www/html/my_site/cron.php
At a basic level, you can work out what to do in cron.php by running modulo operations on the current timestamp, e.g.
if (time() % (60*60)) {
// runs every hour, on the hour
}
if (time() % ((60*60)/2)) {
// runs at xx:00 and xx:30
}
if (time() % (60*5)) {
// runs every five minutes
// although it'll actually only run every 15 mins, because cron.php is called every 3 mins!
}