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
Related
How to run cron jobs one after another, like job queue ?
I am working on project which has near about 20+ cron jobs, most of them starts every hour and are very DB intensive, so i would like to have something like job queue which will start one cron job after another ,so not all 20+ job start at the start of new hour and make my server unavailable.
Is there per-built system for something like this ?
or any other easy way to add job queue functionality to the linux cron. ?
Write a bash script to run the commands in sequence, and add that to the crontab.
Eg create a file called something like phpjobs.sh with the commands to run the jobs in order:-
#!/bin/bash
/usr/bin/php -q <yourfile1.php>
/usr/bin/someothercommand
/usr/bin/php -q <yourfile2.php>
/usr/bin/php -q <yourfile3.php>
then make it executable
chmod 755 phpjobs.sh
Then add it to the crontab to run every hour
0 * * * * root /path/to/phpjobs.sh
I m trying to configure a cron job via cpanel. What i want to do is execute a php script that updates a value in a mysql database table every minute. i tryed 2 cron commands :
wget -O – -q "URL_HERE"
and
usr/local/bin/php -q /home/user/public_html/filename.php
note that its a very small script with tiny execution time, so it should be ok.
for testing purposes I configured it to run every minute to see if it works.
Here is the problem:
the cron job runs ok for the first two times , but then it stops. for example if the time is 12:00, the script will run on 12:01, then on 12:02, but NOT on 12:03 .. 12:04 and so on. What could be the problem here?
thanks in advance
Make sure your crontab entry is correct...
For running a cron every minute, one needs to add entry something like
* * * * * "username" "task to execute"
Using PHP, I want to have scheduled tasks based upon the time the server is currently running.
Say at 7pm on Sunday I want a database query to be ran.
The way in which I've considered doing this is to put the task in the script that is ran on each page load in the session init.
Any ideas?
One method to automatically run a PHP script at specified time intervals is to use Crontab. This can be particularly beneficial for scripts that need to automatically update information without user interaction such as a script that gathers website statistics so that they can be emailed to you or a script that regularly retrieves content from another website.
See: PHP CLI and Cron
You can just use cron to trigger your script for you with something like this:
If you have ssh access you can add a crontab entry like this:
crontab -e
and enter something like this:
0 19 * * 0 php -f /path/to/script/file.php
where 19 is the hour (7pm), 0th day of the week (sunday), and 0 is the minute.
this will run at 7pm on sunday.
This is what your cron tab is for. You can specify whatever tasks, scripts, or other programs you need to run at whatever time you want to run them. Cron is run by your operating system and goes by your operating system's clock.
For example if I wanted to run a PHP script every minute, I would put something like the following into my cron tab.
* * * * * php /path/to/script.php
How you actually create cron entries depends on what your server setup is like. If you have some sort of shell access on a Unix/Linux system, you can edit your cron tab easily by running the following command.
crontab -e
That will bring up the crontab in the default text editor.
If you are using something like cPanel, you will have to consult the manual for information on how to edit cron entries.
The actual syntax for the cron can be complicated at first. There are entry generators online that you can use if you need help.
0 * * * * php /some/script.php # This will execute at the 0 minute of every hour.
0 1 * * * php /some/script.php # This will execute at the 0 minute of the first hour of every day. In other words, every day at 1AM.
You should use your systems Cron deamon to schedule and run a PHP file.
First read up on Cron:
https://help.ubuntu.com/community/CronHowto
Then implement this:
Open crontab:
vim crontab -e
Add an entry to your cron table:
0 19 * * 0 /path/to/php /path/to/script.php
Your script.php will contain the code/logic to query the database.
I'm working on a site in php. Is it possible to send cron jobs at times defined by an algorithm? Could I add such a functionally in the console or send it from my php script? I haven't used cron yet.
The first thing that comes to the mind - run a cron job every minute with php script which generates random number in order to check whether to run the rest of the job.
This line in crontab will run cron job every minute (replace paths with yours)
*/1 * * * * /usr/bin/php /path/to/job.php
Or if you can't directly call php, you may use curl or wget like that
*/1 * * * * /usr/bin/curl --silent --compressed http://yoursite.com/job.php
And in your php file do whatever you want to check if it's time to run the rest of the job, like that:
if(rand(1, 60)==1)
{
include 'the_actual_job.php';
}
Set your PHP script to be executed every hour, for example. Then add this code at the top of your script
sleep(rand(1, 60));
Now your script will be executed every hour + some seconds. It is random :)
If you want to schedule a job at a particular time you can use "cron"s friend "at" which will schedule a script a the time requested.
You simply fire off a command like:
$reqtime = '17.00'
system('at -f yourscript.sh ' . $reqtime);
And yourscript.sh will magically run at 17.00 today
i have a php script that should be run automatically every day.
as the php script is run on a request,how can i do it?
is there any way else using cronjob task?
Two options:
Use crontab demon
Hire a worker and make him open script in a browser every 24 hours
The choice is yours :)
To use crontab, type crontab -e in console, the text file opens. Add a line at the end:
0 0 * * * /usr/bin/php /var/www/mysite/httpdocs/daily_stats.php
Where:
0 0 * * * - run every day at 00:00
/usr/bin/php -path to your PHP (can be determined by which php command)
/var/www/mysite/httpdocs/daily_stats.php - path to your PHP script
if which php outputs nothing, install PHP cli by running:
sudo aptitude install php5-cli
Good luck!
use cron job option who start automatically and give result before 24 hours
Use the cron job, this is the best solution.
otherwise, you can run an infinite loop inside php and sleep 24 hours.
horrible solution though.
If cron isn't available in some sort of way you could use Google app engine's cron for this. Because cron is the way to go.
If cron is not available you could execute a php script in CLI which will run all the time.
In the script you can make a infinite while loop.
In the while loop, check a file on disk or a db record (you can controll this file or db record from an external script, telling the looping script what to do (CLI execute another script at a given hour) and when to exit)
If you use a database,don't forget to initialize and close the db connection each time the loop runs.
I'd sleep the loop every 1 min or so.. you could use this instead of linux cron for many more things.