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.
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 am trying to run in mac a php script using cron. I want this php script to run every one minute. I read several sources online and from my understanding is better if I use launchd. In any case, I try to make it work with cron and then if it works fine I might try to use launchd.
So here is what I do:
I wrote this command in a txt file:
* * * * * /usr/bin/php /Applications/MAMP/htdocs/php_test/main_script.php
There I have the path to php (I found it with the "which php" command) and the path to my php script.
I named the txt file: crontasks.txt and I run it through terminal with this command:
crontab /Users/dkar/Desktop/crontasks.txt
When I list the crons (crontab -l) I see that this job is listed. But nothing comes as output every one minute. The output is supposed to be an image stored in a specified folder. What am I missing here?
Thanks in advance
D.
You might have a slight miscomprehension how crond processes the "crontab" files. It would suffice to run the command crontab -e, which would let you edit your personal crontab or you edit the system crontab.
If you use crontab -e it will open the default editor (vi/vim) and you can enter the line:
* * * * * /usr/bin/php /Applications/MAMP/htdocs/php_test/main_script.php
Then you simply save your file. If you want to use the system crontab you will have to edit it directly and enter the line. Additionally the system crontab has one more field for the username. Like this:
* * * * * /usr/bin/php root /Applications/MAMP/htdocs/php_test/main_script.php
On the next full minute your script will be executed by crond.
I have a database with a bunch of links that I want to keep updated. Basically if a link returns a 404 error code I want to remove it from the database. I have a script that I am using however I need to run it manually. How can I make this work using CRON?
in your shell as cron user (or root):
crontab -e
This will bring up your crontab file in your editor. Add a new line something like this:
* */12 * * * /path/to/script
Save/exit the file.
Now for a quick lesson on cronjobs:
-The first 5 arguments in the line tell how often, or when the cron daemon will execute the 6th argument.
-From left-right, arguments represent: minutes, hours, days, weeks, months
-An asterix (*) tells the cron to run on all values of it's associated time measurement (example * * * * * means to run every minute, of every hour, of every week, and of every month!)
In my example, * */12 * * * means to run every 12 hours.
Check out: http://kevin.vanzonneveld.net/techblog/article/schedule_tasks_on_linux_using_crontab/
To run a PHP script with cron you can use the PHP executable and the path to the script.
On most linux systems you want to edit your cron file (the crontab) with the command crontab -e. This will open up a command line based editor and you can just append your new job to the bottom of the file using this format.
<minute> <hour> <day_of_month> <month> <day_of_week> php /path/to/script
If the commands dont work for you let me know what distribution you are using and I can modify the instructions.
/usr/bin/php -q /home/user/public_html/script.php
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