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.
Related
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.
Currently I have a parser.php which loads an xml file and inserts new data from the xml file into a mysql database. How would I go about refreshing this php file every 30 seconds so my mysql table always has fresh data? I think I could use short-polling to do this, but I'm guessing this is not the most efficient of options.
Thanks in advance
This is a non-PHP solution which will require you to have shell (SSH) access in order to run the script, however you can also run it through PHP with exec() if you want to. Shared hosting environments might present a challenge for this approach but as long as you can execute scripts under your user credentials you should have no problems running it.
First you will need to create a bash script with the following content and save it (I'll use the name parser.sh for the purpose of this example). You can then adjust the timeout in the sleep 30 line if you want to.
#!/bin/sh
while true
do
php parser.php
sleep 30
done
In order to run the script you'll need to give it execute permissions.
chmod +x parser.sh
Now you can use the nohup command with the ampersand (&) argument to ensure that the script will run in the background even when a termination signal is sent after, lets say, closing the shell (SSH). The ampersand is important!
nohup ./parser.sh &
Now you can use top or ps aux | grep parser to ensure that the script is running. As I already said before you can also use PHP exec() to start the process but shell is still the preferred and most reliable way to do this.
If you want to stop the background process which executes your script then you'll simply have to kill it. Just use ps aux | grep parser to find out the PID of the parser process (its in the second column to the left) and use it with the kill command.
kill 4183
You need to use a cronjob, but crons jobs runs every 1 minute or more.
Another way is to make a "daemon".
Very basic example:
<?php
while(true) {
// check if 30 seconds has passed.
// then execute some function...
};
?>
Then you need to execute this in your terminal:
$ php script.php &
This link should help.
Greatings!
I have Linux, PHP installed.
I want to set the cron job which will be executed 11:00 AM.
We can add cron job using crontab command
time of execution /usr/bin/php /var/www/html/somefile.php
I have experience about adding cron job like above using file path but now I need to add file url in cron job with some query parameter.
something like
time of execution /usr/bin/php www.example.com/somefile.php?do=sometask&with=me
I have tried above method with url also but it is not executing.
Please suggest
But
Many options. You could use curl.
/usr/bin/curl www.example.com/somefile.php?do=sometask&with=me &>/dev/null
I'm working on a wamp development environment and testing how long it takes to get the site indexed. I'm doing this by running cron manually.
The problem is that if there's 700 jobs in the job_queue, each run of cron does only some of them, so I need to run cron several times. How could I keep calling cron in a loop until there are no more jobs left in the job_queue?
Also, I'm open to any drush alternatives. I'm aware of drush cron, but this also does only some of the jobs each run, so needs to be run again manually.
If you want to run something all at once until it's done, cron is the wrong tool for the job; batch API is the tool for that. Unfortunately the search module is written to update only on cron, but there's not a lot of code in search_cron to copy into a batch function. So I'd suggest going the batch route rather wrapping some sort of pseudo-batch around cron, as your end goal doesn't seem to involve cron at all.
step 1. - Run cron every minute
step 2. - Just check if the script is already running. If so - close the "new script"
$lid='';
if(is_array($argv) && isset($argv[1])) {
$lid=$argv[1];
}
if($lid!=='') {
$xx=array();
exec('ps x | grep "thefile.php '.$lid.'" | grep -v "grep"', &$xx);
if(count($xx)>1) {
die('Script '.$lid.' running already... exiting... '."\n");
}
}
Put it in cron with every minute php thefile.php 1
php thefile.php 2
php thefile.php 3 to run 3 scripts at the same time.
drush search-index will generate the remaining search index for you.
I want to mail after 6 hours automatically to my user who hasn't fully completed form on my website.
Help Me
Use crontab -e to edit the cron table for your account.
In the crontab, put an entry something like...
0,10,20,30,40,50 * * * * /usr/bin/wget -O - -q http://path.to/cron.handler.php
or the equivalent
*/10 * * * * /usr/bin/wget -O - -q http://path.to/cron.handler.php
...which will run the cron handler php file every 10 minutes using wget (there are other options as well, and you may need to edit the command appropriately). (Note: you don't want to just run it every 6 hours, because then if someone happened to fill out the form right after it ran, it wouldn't have been 6 hours since they filled it out next time it runs, so you'd end up with 10-11 hour gaps.)
Then in your PHP file, find users who BOTH (a) haven't fully completed the form for at least 6 hours and (b) haven't been emailed yet. Send them an email, and mark them as having been emailed.
You will need to create the php script that does the checking and mailing, and then set the cron job like the following
/path/to/php -q /home/username/public_html/mycheckingscript.php
Obviously you will need to adjust the first path to point to your php binary, and the second path to point to the full location of your checking & mailing script.
I don't think you want to set the cron up using php. Instead write a php script and then have cron execute that script every hour or so. This would be something that is going to be dependent on your operating system.
For linux, here is the manpage for using crontab.
There is no way you can change/add a schedule on the cron job on the fly. according to my experience. because until now i did not find..