How to use bash file in centos crontab - php

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}

Related

How to run cron job in php

I have php function that I wnat to run every 15 minutes. I found some questions like this:
How can I make a cron to execute a php script?
and in my case I should use this:
*/15 * * * * /usr/local/bin/php -q /path/to/my/file.p
But should I run this command in terminal or put it in my file? And once, it is executed, will it run all the time or will have time limit?
Thanks!
PHP doesn't run cron jobs, your server (or operating system) is doing this. There are two ways to put your cron job to work:
#1
Using the shell command crontab. The command crontab -l will list all existing cronjobs for your user (most likely there are none yet). crontab -e will open an editor window where you can put in a you cron job as a new line. Save, and your cron job is now running. crontab -l again and you will see it listet. crontab -r to remove all the cont jobs.
You can also start a cron job from a file. Simply type crontab filename (eg. crontab textfile.txt)
Alternatively you can also start it from within PHP. Just put your cron job into a file and start it via exec() like so:
file_put_contents( 'textfile.txt', '*/15 * * * * /usr/local/bin/php -q /path/to/my/file.php' );
exec( 'crontab textfile.txt' );
#2
If you have admin privileged on your system you can create a file in /etc/cron.d/ (for example, call it my_cronjob) and put your cron job there. In this case you probably want to run it as a user (not as admin, that would be rather insecure). This is quite easy to do, just add the user name like so:
*/15 * * * * user_name /usr/local/bin/php -q /path/to/my/file.p
(In this case the cron job will not be listet under crontab -l)
Answering your second question: As long as the cron job is listet in crontab -l or as long as the file is sitting in /etc/cron.d the cron job is running, in your case, every 15 minutes.
10 * * * * /usr/bin/php /www/virtual/username/cron.php > /dev/null 2>&1
There are two main parts:
The first part is "10 * * * *". This is where we schedule the timer.
The rest of the line is the command as it would run from the command line.
The command itself in this example has three parts:
"/usr/bin/php". PHP scripts usually are not executable by themselves. Therefore we need to run it through the PHP parser.
"/www/virtual/username/cron.php". This is just the path to the script.
"> /dev/null 2>&1". This part is handling the output of the script. More on this later.
Please read this tutorial http://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800

Run 50 php scripts every 5 minutes

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

Execute PHP script in cron job

In our centos6 server. I would like to execute a php script in cron job as apache user but unfortunately it does not work.
Here is the edition of crontab (crontab -uapache -e)
24 17 * * * php /opt/test.php
and here is the source code of "test.php" file which works fine with "apache" user as owner.
<?php exec( 'touch /opt/test/test.txt');?>
I try to replace php with full path of php (/usr/local/php/bin/php) but also it doesn't work.
Automated Tasks: Cron
Cron is a time-based scheduling service in Linux / Unix-like computer operating systems. Cron job are used to schedule commands to be executed periodically.
You can setup commands or scripts, which will repeatedly run at a set time. Cron is one of the most useful tool in Linux or UNIX like operating systems. The cron service (daemon) runs in the background and constantly checks the /etc/crontab file, /etc/cron./* directories. It also checks the /var/spool/cron/ directory.
Configuring Cron Tasks
In the following example, the crontab command shown below will activate the cron tasks automatically every ten minutes:
*/10 * * * * /usr/bin/php /opt/test.php
In the above sample, the */10 * * * * represents when the task should happen. The first figure represents minutes – in this case, on every "ten" minute. The other figures represent, respectively, hour, day, month and day of the week.
* is a wildcard, meaning "every time".
Start with finding out your PHP binary by typing in command line:
whereis php
The output should be something like:
php: /usr/bin/php /etc/php.ini /etc/php.d /usr/lib64/php /usr/include/php /usr/share/php /usr/share/man/man1/php.1.gz
Specify correctly the full path in your command.
Type the following command to enter cronjob:
crontab -e
To see what you got in crontab.
EDIT 1:
To exit from vim editor without saving just click:
Shift+:
And then type q!
I had the same problem... I had to run it as a user.
00 * * * * root /usr/bin/php /var/virtual/hostname.nz/public_html/cronjob.php
You may need to run the cron job as a user with permissions to execute the PHP script. Try executing the cron job as root, using the command runuser (man runuser). Or create a system crontable and run the PHP script as an authorized user, as #Philip described.
I provide a detailed answer how to use cron in this stackoverflow post.
How to write a cron that will run a script every day at midnight?
I tried all combinations with PATHs, but don't work. Probably they are needed.
In my case, with Centos 7, a reboot or server worked.

Triggering in php mySql

Let I have a table as the following...
-----------------------------
| id | questions | type |
------------------------------
Based on server time I want to pick 5 random rows from this table and place those into another table.And I want this to happen after every 15 minutes of the server time. I am using php mySql. How this can be done. Is there any way so that it will run in localhost (I am using windows) and linux server. If anybody of you have the idea please help me.
Thanx
Assuming you have linux server and cronjob installed:
STEP 1: Open a Terminal Window
Once you have opened a terminal window, type in the following...
crontab -e
STEP 2: Add a cron job
At the end of the file paste one of the following to have your cron job run...
Every Minute:
* * * * * /path/to/php -f /absolute/path/to/script.php
Every Five Minutes:
*/5 * * * * /path/to/php -f /absolute/path/to/script.php
Every Hour:
0 * * * * /path/to/php -f /absolute/path/to/script.php
Every Five Hours:
0 */5 * * * /path/to/php -f /absolute/path/to/script.php
STEP 3: Save
And in your php file you can do those manipulations you stated as you wish
You need a scheduler. You can write a PHP script to program your requirement. Since you are running Windows, start your Windows Task Scheduler, add your script as the action.
If you were running Linux, you would be doing what #Vit Kos noted above.

How to automate my cron php script

I'm using PHP and was wondering how can i run my PHP script to run at midnight and check if a member has been inactive for six months if so delete.
See Crontab: http://en.wikipedia.org/wiki/Cron
example crontab entry for 12:01 am:
1 0 * * * /usr/bin/php /home/whatever/myscript.php > /home/whatever/outlog.txt
command to edit the crontab entries for the logged in user:
crontab -e
A cron line like the following should suffice:
0 0 * * * /usr/bin/php /path/to/script
I would advise that you run the script at an irregular time (i.e not on an hour or day boundary), as you may get to the situation when you have too many scheduled tasks occurring at the same time.
If the script generates any output, then that will typically be emailed to the user mailbox. So, you should design your script to not generate output in the normal case. Or, alternatively redirect the output to a file or the syslog.
I use syslog redirection quite a lot. For example the following will log the commands output into the syslog using the tag my_script.
20 1 * * * /usr/bin/php /path/to/script | /usr/bin/logger -t my_script

Categories