I have 50 Wordpress installations running on 1 server. Every Wordpress website has a script which must execute every 5 minutes. I don't want to use the build-in cron of Wordpress and want to run the scripts from cron. What is the best solution?
*Solution 1: create a cron job for every individual wordpress installation *
5 * * * * /usr/bin/php /wordpress-site-1/cron.php
5 * * * * /usr/bin/php /wordpress-site-2/cron.php
5 * * * * /usr/bin/php /wordpress-site-3/cron.php
... + 47 other scripts
*Solution 2: create 1 cron job and run a bash script *
5 * * * * /run/the-script
The script:
#!/bin/bash
# include the 50 sites in array
array=( wordpress-site-1 wordpress-site-2 wordpress-site-3 ...)
for i in "${array[#]}"
do
{execute php file)
done
By far, having 50 singular crons is much more efficient than have it run in one script. The only downside is that you'll have to insert 50 lines in your crontab. But hey, if that's everything...
Reasons for having 50 crons instead of one:
If one of the crons fails, the others will still run
If one of the crons fails, you'll be notified and don't have to do complex log-analysis to find out which one of your crons failed
You don't have to worry (or worry less) about script timeouts, memory usage, etc: 50 scripts executed in one time is about 50 times heavier then having them executed one by one
You can divide payload on the server
(You can probably think of some more)
As for the last point (dividing payload), I'd suggest to execute the first 10 scripts at zero, 5-past, 10-past etc, and the next 10 scripts a minute later. This way they don't run all at once, causing a high peak-load every 5 minutes:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /usr/bin/php /wordpress-site-1/cron.php
... cron 2 - 10
1,6,11,16,21,26,31,36,41,46,51,56 /usr/bin/php /wordpress-site-11/cron.php
... cron 11-20
... etc.
This is a pattern that you could use:
0,5,10,15,20,25,30,35,40,45,50,55 * * * *
try this solution:
cat cronjob
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /usr/bin/php /wordpress-site-1/cron.php
.
.
.
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /usr/bin/php /wordpress-site-50/cron.php
Then:
chmod +x cronjob
chmod +x /wordpress-site-1/cron.php
.
.
.
chmod +x /wordpress-site-50/cron.php
/etc/init.d/crond start #redhat based servers like centos
/etc/init.d/cron start #debian based servers like ubuntu
crontab cronjob
NOTE: You can use for loop to make it easier.
if you want every script run per 5 min, you can do this:
*/5 * * * * /run/the-script
Related
i want to run 50 php page from crontab every minute what is the best way
insert every one in crontab or use bash to load all from one corn
this is example what i need
1 * * * * /usr/bin/php /var/www/folder/page.php?id=1
1 * * * * /usr/bin/php /var/www/folder/page.php?id=2
1 * * * * /usr/bin/php /var/www/folder/page.php?id=3
etc ...
so can me use query link like this ?id=1 in crontab or bash file
Thanks
You probably should look for an alternative to running 50 scripts per minute, every minute. As Ed Heal points out, there is likely a much better way to design the system to do whatever it is that you are trying to do.
For what its worth, here is a single bash script that runs those 50 commands in sequence:
#!/bin/bash
for i in {1..50}
do
/usr/bin/php "/var/www/folder/page.php?id=$i"
done
Alternatively, to run those 50 processes in parallel:
#!/bin/bash
for i in {1..50}
do
echo "/usr/bin/php '/var/www/folder/page.php?id=$i'"
done | parallel
This requires that you have GNU parallel installed. For information on how to optimize parallel for your system and requirements, see man parallel
Shorter version of John1024's GNU Parallel example:
parallel /usr/bin/php /var/www/folder/page.php?id={} ::: {1..50}
I wrote a PHP script that push a CSV file into a database. I want to do this automatically every minute. I know there is a way via cron on Linux but I don't know anything about bash and think cron can't give my PHP file a callback, so I can show a progress bar for the user to see the timer interval. What do I do?
You can enter your jobs using crontab -e. If your default editor is vi, I recommend to change it nano using export EDITOR=nano because it is easy to use for starters.
Every line of the crontab file represents a job. The first 5 tokens are : minute, hour, day of month, month, day of week respectively, the last one is command so in your case first 5 tokens will be * * * * * that means run this job every minutes when the second is '00'.
You can call your php files directly using this command : php /var/www/cron.php & or using a browser wget -O /dev/null http://example.com/cron.php If you use first one you cannot use some $_SERVER variables but if you use second one, it is like a real browser.
In your case you can use like this :
* * * * * wget -O /dev/null http://example.com/cron.php
to add a cron and make it run every minute, type crontab -e and add the following line
* * * * * command you need executing
example:
* * * * * ls -l /home/ > /usr/local/users.txt
* * * * * df -h > /tmp/filesystem_usage.txt
* * * * * service httpd restart
Look at this for a starter: http://kvz.io/blog/2007/07/29/schedule-tasks-on-linux-using-crontab/
Also remember that cronjobs don't support all $_SERVER vars like 'DOCUMENT_ROOT' and 'HTTP_HOST', so try to avoid them, or use a workaround.
Some 'callback' possibilities:
- Let your script trigger another script
- Redirect the output of your cron to an another bash script
- ...
I currently have a system where on the client side this Update function is called every X amount of seconds:
var Update = function(){
$.getJSON("PHP/Update.php",{
userid: userID },
function(json){
doSomething();
});
}
In the PHP file some values are updated and these new values are sent back to the client.
<?php
session_start();
include 'dbconnect.php';
$userid = $_GET['userid'];
//Change some values in the database
//Add these new values to $json array
echo json_encode($json);
mysqli_close($link);
?>'
This works great when there is exactly one client active at all time. Not so much when there are multiple or no clients active. So what I want is that this PHP files gets called once every X amount of time, regardless of how many clients there are. I realize I probably have to create a loop in PHP but I just can't get it working. Can anyone tell me how I'm supposed to do this? Thanks in advance.
The tool you use to schedule things in unix-style operating systems is cron. If your server runs MS Windows, you'll need to use some other scheduler.
There are a couple of ways that you can use cron to run your script.
First off, if you have a CLI version of PHP available (as opposed to an Apache module or a fcgi version), you can run it directly. On my system, a cron job to run the script once per minute might look like this:
* * * * * /usr/local/bin/php /path/to/your/script.php
Note that on your system, the php binary might be located in /usr/bin/ or /opt/local/bin/ or somewhere else. You'll need to check with your operating system's package repository to find out where, or type which php from a terminal.
If you use a "shebang" at the beginning of your script, you can get away with a simpler cron job:
* * * * * /path/to/your/script.php
More information about running PHP shell scripts can be found here. A word of warning though... If you launch scripts directly like this, they will run as the user who owns the cron job, which may have security (or at least permission) implications. If you don't understand what this means, please ask more questions before doing it!
If you don't have a command line PHP available, you can still have the PHP script run via cron, by fetching it from a command-line HTTP client like curl or wget or fetch (depending on your operating system). For example:
* * * * * curl -o /dev/null http://example.com/path/to/script.php
or
* * * * * wget -O /dev/null http://example.com/path/to/script.php
On a unix system you can man crontab for instructions on how to install cron jobs, and probably man 5 crontab (depending on your operating system) for instructions on the format for setting specific times. Note that cron uses 1-minute granularity. If you want greater frequency than that, you can fake it. For example, for three times per minute:
* * * * * /path/to/your/script.php
* * * * * sleep 20; /path/to/your/script.php
* * * * * sleep 40; /path/to/your/script.php
And if you want lesser frequency, you use crontab's documented format. For example, every five minutes on weekdays but every 10 minutes on weekends:
*/5 * * * 1-5 /path/to/your/script.php
*/10 * * * 6-7 /path/to/your/script.php
I need to run a php file on every 2 hours, so i am using this command
* */2 * * * /usr/bin/php /var/www/html/sports/webservices/rss-insert.php
i am also using this
* */2 * * * php /var/www/html/sports/webservices/rss-insert.php
But both are not working. can anyone help me.
Thanks
The crontab you have made will run every minute of every second hour rather than every two hours.
In order to get it to run every two hours you need something like this:
5 */2 * * * /usr/bin/php /var/www/html/sports/webservices/rss-insert.php
which will run it 5 minutes after every second hour e.g 2:05, 4:05...
This is assuming your script is able to run. Try running the command portion from the cron by hand at the command line and make sure it does what you want.
This works for me definitely work for you.
0 0-23/2 * * * php /var/www/html/sports/webservices/rss-insert.php
this might be a repeat.
i would like to run my index.php every certain mins. My server has option run cron command/ cron jobs. Someone please tell me what could should I use to schedule.
Thanks in advance.
First of all, why?
Second, crontab -e will allow you to edit your crontab. Then it's just as easy as
? * * * * php /path/to/index.php
You could use this command
Minutes Hours Day Month WeekDay
45 * * * * php /path/to/index.php *
Here is an article about a php cron manager
First, here is how cron works:
http://en.wikipedia.org/wiki/Cron
Then, the comand should look like this:
* * * * * cd /directory/of/the/script/ && php scriptname.php >> /directory/of/the/logfile.log