This question may seem repetitive, as there are many threads around with the same subject, but thing is that most solutions seems to be linked with terminal coding, which i'm not comfortable with. The problem is simple i have a php script that needs to be executed very 10 seconds. Cron job in cpanel allows only upto 1 minute. What's the workaround to let cron work every 10 seconds ?
Let the cron job run after every minute and in your php script the following code example might help you out. I have used counter limit to 6 because this script will run after every ten seconds and six times in one minute.
<?php
for($i=0;$i<6;$i++){
sleep(10);
task();
}
function task(){
}
You can use simple bash-script like
#!/bin/sh
while [ true ]
do
php script.php
sleep 10
done
CRON jobs are the standard way to run some tasks periodically. Setting cron jobs require access to the terminal. However, some shared hosting providers don't provide this and you need to set it up through their interface.
If you hosting don't provide this you can third-party services that will call you url every 'X' seconds.
Here are few of them:
https://azure.microsoft.com/en-us/services/scheduler/
https://atrigger.com/
You can Google for more
Note: You can have publically expose the PHP file as an url
You can use the GUI Cpanel, select "once per minute" an try with something like the next command:
/path/to/bin/php /path/to/script.php; sleep 10; /path/to/bin/php /path/to/script.php; sleep 10; /path/to/bin/php /path/to/script.php; sleep 10; /path/to/bin/php /path/to/script.php; sleep 10; /path/to/bin/php /path/to/script.php; sleep 10; /path/to/bin/php /path/to/script.php
Related
I can create a bash script that runs a PHP file over and over with a 3 second delay like this:
php -q /home/script.php
sleep 3
php -q /home/script.php
sleep 3
php -q /home/script.php
But I'm looking for a better way to do this, so I don't have to create a file with hundreds of thousands of lines and then check to see when it's done so I can just restart it.
Is there any way to create a loop that runs a PHP file and once it's done, it just does it again - for an infinite amount of time (with a 3 second delay between each run)?
Using Cronjob
What Are Cron Jobs? https://www.hostgator.com/help/article/what-are-cron-jobs
A cron job is the scheduled task itself. Cron jobs can be very useful to automate repetitive tasks.
Some useful tool for cronjobs: https://crontab.guru/
An example: "run a script every 1 minute"
*/1 * * * * bin/php /path/to-your/script.php
Looping
In case you really need to repeat a task each X seconds, you could write a while loop for that:
#!/bin/bash
while true; do
# Do something
sleep 3;
done
You may consider writing a while loop:
while true
do
php -q /home/script.php
sleep 3
done
I want to run my cron script at sheduled intervels created at blocks/plugin_name/cron.php . Please help me how to create and call this cron script to run at regular intervels.
Cron.php is the old way, you should create a function plugin_name_cron() in blocks/plugin_name/lib.php
Then in version.php you have a cron parameter which will tell Moodle to run the plugin every x seconds.
$plugin->cron = 0; // Seconds.
So every 15 minutes is
$plugin->cron = 15*60; // Seconds.
0 seconds means never run the cron.
This does depend on the admin cron being run too. If the admin cron is run ever 60 minutes then your plugin cron will only be run every 60 minutes.
For testing you can run the admin cron manually from http://yoursite.com/admin/cron.php
If you want to schedule the cron to run at a certain time, then you will need to add some code to your cron function to work out the scheduled time.
EDIT:
Actually the cron function is slightly different for blocks. Create a function cron() in your class class block_plugin_name extends block_base..
UPDATE:
From Moodle 2.7+, the above has been replaced with scheduled tasks https://docs.moodle.org/dev/Task_API#Scheduled_task_usage
I know that for running php script every time (seconds or minute) we can use Cron (job or tab) but cron has a 60 sec granularity so we are forced to have an infinite loop for running php script . For example we can write the code below to call it at the top of the script:
#!/bin/bash
while [ true ]; do
#put php script here
done
but it's illogical because we must change php execution time in php.ini so we have a lot of problems (Security , overflow , ... ) in server . well, what should we do exactly ?
My question is how to run php script every 5 seconds that hasn't got problems in php execution time .
Use Cron job script. Get a 30 seconds interval , you could delay by 5 seconds:
-*/5-22 * * * sleep 5;your_script.php
The above script will run 5am to 10 pm
Another Alternative is,
You need to write a shell script like this that sleeps on the specified interval and schedule that to run every minute in cron:
#!/bin/sh
# Script: delay_cmd
sleep $1
shift
$*
Then schedule that to run in cron with your parameters: delay_cmd 5 mycommand parameters
#!/bin/sh
#
SNOOZE=5
COMMAND="/usr/bin/php /path/to/your/script.php"
LOG=/var/log/httpd/script_log.log
echo `date` "starting..." >> ${LOG} 2>&1
while true
do
${COMMAND} >> ${LOG} 2>&1
echo `date` "sleeping..." >> ${LOG} 2>&1
sleep ${SNOOZE}
done
The above script will run at a second interval.
It will not run the PHP script when it is still processing, also reports the interaction/errors inside a log file.
How about using the sleep function to delay every 5 seconds
int sleep ( int $seconds ) - Delays the program execution for the given number of seconds.
I prefere to use sleep in your PHP
while( true ) {
// function();
sleep( 5 );
}
You can stop it after 1 Minute or something like that and restart it with a cronjob. With this solution you are able to execute your script every second after your last executing and it works not asynchronously.
You save ressources with that solution, because php have not to restart all the time...
I am using the paid host Hosting24 to run my website. I got a cron job which execute the following code every 1 minute.
<?php
require_once('connect.php');
for($c = 0; $c < 60; $c=$c+5)
{
// php to mysql queries SELECT/ UPDATE/ INSERT etc...
sleep(5);
}
mysql_close($my_connection);}
?>
I tried to use the for loop to allow the script to run for 1 minute. Eventually my script should run for as long as I want it to be because the server will execute it every 1 min.
However, I opened my website for a short while and then I cannot connect to it. I cannot even access my cpanel.
Is my cron job script overheating the system, so the system is down?
How should I set up my cron job script to let it run every 1 min and lasts for 1 min?
Thanks.
It's been my experience that cron jobs that need to include files should contain the full path to that file (the CLI environment can differ greatly from the environment inside the web server). Try that and see if it helps.
If not, the next thing you need to do is turn the cron job off and run it from the CLI yourself, using top to look at the process usage. See how long it takes for your cron to run.
I have 3 scripts that do some stuff.
I want to run them continously and concurrently.
Let's say for example:
First script took 30 minutes to finish.
Second - 20 mins.
Third - 5 mins.
So I need everyone of them to run immediately after it's finished.
The 3 scripts make UPDATE in a same DB, but they need to work separately.
They can run together at once, but not couple of times(my english sucks, sorry about that).
Let's explain what I mean with example:
firstScript.php is running
secondScript.php is running
thirdScript.php is running
firstScript.php trying to start but it still running. Wait.(till finish)
May be some shell script will do the job, but how?
Make a bash script that takes one argument, and have it do something like this:
if [ -f /tmp/$1 ]
then
echo "Script already running"
else
touch /tmp/$1
php $1
rm /tmp/$1
fi
Set up a cron to run this script and pass it the name of the php script you want to run.
You could execute a shell command just before the php script dies. Example :
while($i < 1000)
{
$i++;
}
shell_exec("bin/php.exe some_script.php");
If you are working on a shared hosting account this might not work do to security issues.
note the "bin/php.exe" need to be edited for your server, point to where ever your php is installed.