I am pretty new to PHP but I want to crate a website uptime checker which should email me whenever my websites are down.
This means that the script should run non stop. Can this be done in PHP? How?
No. You want to run a script at a set interval instead. Cron is what you want.
Here's a tutorial to get started. http://net.tutsplus.com/tutorials/php/managing-cron-jobs-with-php-2/
You could use a cron task to schedule your script running at regular intervals. Or if you are hosting on Windows use the Windows Scheduler.
Normally there's an execution time limit that will stop scripts from running longer than a specified time. A better solution would be to set up a cron job that executes the script periodically.
you can do it by a cron job (a script to check your website) and make it run at regular intervals.
Related
I don't know how to call this.
if I wrote a php script to calculate an interest for saving account.
how can I tell the program to run the script on the end of the day without user monitoring. such as 0.00am or the server start.
If you are on UNIX server, Cron is the way to go. On Windows machine use Scheduled tasks.
Call the PHP file via CLI using Crontab, if you're on Windows try pyCron.
you need to run cron jobs look wiki cron for more information. hope this helps.
This question already has answers here:
PHP: running scheduled jobs (cron jobs)
(13 answers)
Closed 5 years ago.
I have a PHP script that checks my database's code for validity. How can I have this run everyday at a certain time so then I can just have it run on autopilot.
What you want is called a cron job and is already covered here:
PHP: running scheduled jobs (cron jobs)
If you are using cPanel on your host, you can go to Advanced --> cron jobs.
Then inside there you can set a cron job command like this:
wget http://yoursite.com/yourfile.php
which http://yoursite.com/yourfile.php should point to your desired function.
You can also set repetition time interval over there.
This way yourfile.php is called in specified intervals.
CronJobs feature are often disabled on most free or cheap Linux hosting. If you could use cron you could use that.
Alternatives:
Use services like : http://www.setcronjob.com/ (many exist, just search on google) , to schedule your task for free(1x per hour).
If you need a little bit more juice you should look into google app engine cron. In the cron you just do a simple asynchronous request to call your page.
There's a cli interpreter called php. Run your script through that as a cron job.
If you are a linux/unix user, i recommend you use CRON .
May it will helps you.
You can schedule a task in Windows - control panel->administrative tools is where you find task scheduler.
run a php automatically after every hour
Thanks
If you have your own server you can run a cronjob(unix/linux) or a scheduled task(windows).
If your app/site is hosted on a shared host that does not offer cronjobs you an use a sevice like
http://www.onlinecronjobs.com/
I have not tested this one but If you google a bit(fee conjobs) you will find plenty of those.
They will call a certain a url accoding to a fixed schedule. Most of those sevices have a fixed set of ip addeses so you can pevent the scipt from being called by anyone else.
You can easily use your database to save the last execution of the script.
Each execution of a certain page you run a script that does the following :
Check if more than 8 hours (example)
Checks in the database if the script has already been executed
If executed: does nothing.
If not executed: starts and updates the date last execution in database.
Is it possible? E.g. A script every minute write a word in a file (without the page opened in a browser).
Yes, you'd need to set up a cron job that runs regularly (eg: every minute).
You can accomplish this with a cronjob.
Get some detailed info here:
http://service.futurequest.net/index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=30
Plus you might want to ask your hoster how you can add cronjobs on your particular server.
This is kind of job definitely not for web server. The job for web server is to invoke your script on request from the web. PHP engine can run autonomously, without server. You can run it: /usr/bin/php -f yourscript.php, on Windows repace beginning with your php.exe path. Then use cron to schedule execution
You can execute php from the command line, in this case your scripts executes regardless of server/browser, you can use a for loop, combined with sleep to achieve what you described.
I have a database in MySQL which have entries of time. There are more than 1000 entries of time. I want to extract time and run a PHP script exactly at that time..
I have tried to run a PHP script continuously which check the time, but my server does not allow to run the script for more than 60 seconds.
EDIT. I have to check the database every second. Is there any alternative?
Use the pear package, System_Daemon
http://pear.php.net/package/System_Daemon/
Try Unix's cron.
You'll need some external service to execute the script. On a Unix box, that would be cron. On a Windows box, use Task Scheduler.
Have you thought about writing your process as a server daemon. It would start up and run in a while loop forever. Every few minutes or however often you'd like it could check the next x minutes of run times. You queue up your requests and whenever that time comes around you kick off the script you need to run. I don't think cron is what you'd want since you are trying to schedule future events at arbitrary times... And I'm sure it's what you are currently using to try and check the db every second.
Write a PHP script to read from the database and add entries to your crontab to make the script run at the desired time
Keeping a process running is not a very good solution, not least because you'll need to ensure it does keep running. Presumably you know when the next occurrence is going to happen - so use the 'atd' to schedule it - when triggered the script should also work when and how to schedule the next job.
This does mean that jobs are chained - and failure of one breaks the chain, also the granularity of most implementations of atd can be rather high.
You might want to look at using a more sophisticated scheduling tool like Nagios or a process monitoring type approach like DJB's daemontools or Oracle's OPMN.
C.
You must use the sleep(arg) function that pauses the PHP script for a given time and then continues.
sleep(50); //Pauses
myFunction(); //Runs after the pause
For example, this pauses the script for 50 seconds.
DJB's DAEMONTOOLS is great. So, apparently, is systemd and/or Upstart, though Remnant plays a true wanker.
The recommended Pear package - system_daemon looks great. However, the author now recommends to use new functionality that's readily available in Ubuntu: upstart
See his article on his own blog from 2012
I have a PHP script that needs to be run at certain times every weekday. Is cron or Windows task scheduler the only way to do this?
Is there a way to set this up from within another PHP script?
Depends how exact the timing needs to be. A technique I've seen used (dubbed "poor man's cron") is to have a frequently accessed script (a site's home page, for example) fire off a task on the first page load after a certain time (kept track of in the database).
If you need any sort of guaranteed accuracy, though, cron or a Windows scheduled task is really the best option. If your host doesn't support them, it's time to get a better one.
Apart from cron or Scheduled Tasks, you can have your PHP script to always run it. The process should sleep (within a loop) until the time has reached. Then, the process could execute the remaining functions/methods. Or, you can set up another script (which will act as a daemon) to check for the time and execute the other script.
Well since the web is a pull mechanism you have to have some sort of action that will trigger a PHP script to execute. cron is an option on *nix and task scheduler on windows. You could also write your own service that has a timer but only if needed, this is common on windows services for updaters, jobs etc.
One way you could do it is in the cron task just call a php script for each action needed. Or one php script that executes other tasks. The problem with web based tasks though such as PHP is timeouts. Make sure your tasks are under 60-90 seconds. If not you might look at using python , perl or ruby or even bash scripts to do the work rather than the PHP script.
cron seems like the best option for you though. You will have to call your script with wget. There are examples here: http://www.thesitewizard.com/general/set-cron-job.shtml
For instance this runs the script everyday at 11:
30 11 * * * /usr/bin/wget http://www.example.com/cron.php
Cron, of course, is by far the best way to schedule anything on *nix.
If this is in a remote server you do not have cron access to, you can setup cron/windows scheduler on your computer, to open a web browser to the page that contains the script you wish to run
You probably want to use cron (or windows scheduled tasks).
If you really wanted, you could set up another php script to run continuously with an infinite loop (with a sleep command inside the loop, say for 30 seconds or so) and then when you reach your desired day/time execute the other script via a shell command call. While possible, I can't think if a single good reason to use this method rather than cron/scheduled tasks
You can write a long running script that runs your main script in predefined times but it will be very unnecessary, error prone, and it will basically be a "cron rewrite in phph".
Using the real cron itself will be easier and a more robust solution. If you are packaging an application, you can put a file in /etc/cron.d which contains a single cron line running your application.
You'll need to use a cron job (under Linux/Unix) or a scheduled task under Windows. You could have another script running on a continuous basis which checks the time and executes a script at a specified interval, but using the OS-supplied mechanism is easier to manage and resilient to restarts, etc.
The Uniform Server project has some good suggestions on mimicking cron in environments where cron is unacceptable. Still though, if cron is at all an option, use it.