I have a program written in php/html where the user will input a job then after submit, will notify the admin and other users through email. What I wanted is to have a scheduled email where it will notify the admin/users that the job has not yet been done after 12 hrs. I've read about cronjob or cron tab but I don't understand hoe to use it well. Is there another way to do it using phpmailer and not crontab?
yes and no.
PHP by its nature is a stateless script that runs when a user or service requests a url.
The php script runs, does it stuff and then ends, normally there is relatively short timeout either in the PHP config or on the webserver, so you cant have a script running in the background waiting for a specific time to send out your email.
In your situation, you would create a cron job (or a scheduled task on windows) this task/job generally runs on the webserver (although is doesn't have to), the task will load a php script, either as a command or via wget.
So set the task to start at the intervals you require, it will envoke the PHP script, send the emails or what ever you want it to do then the php script will end, ready to run on the next schedule.
Related
First of all sorry to post a question that seems to have been flogged to death on SO before. However, none of the questions I have reviewed helped me to solve my specific problem.
I have built a web application that runs an extensive data processing routine in PHP (i.e. MySQL queries, calculations, etc.).
Depending on the amount of data fed to the app this processing can take quite a long time so the script needs to run server-side and independently from the web front-end.
There is a problem, however. It seems I cannot control the script execution time limit as long as the script is invoked via cgi.
When I run the script via SSH and the command line it works fine for however long it takes to process the data.
But if I use the exec() command in a php script called via the webserver I always ends up with the error End of script output before headers after approximately 45 seconds.
Rather than having to fiddle with server settings (a nightmare in terms of portability) I would like to find a solution that kicks off the script independently from cgi.
Any suggestions?
Don't execute the long script directly from the website (AKA, directly from Apache) because, as you've mentioned, it will block until it finishes and potentially time out. Instead, use the website to schedule a job (an execution of the long script) to be run immediately.
Here is a basic outline of how you can potentially do this:
Create a new, small database to store job requests, including fields job_id, processing_status, run_start_time, and more relevant fields
Create some Ajax that hits your server and writes a "job request" to this jobs database, set to execute immediately.
Add a crontab script or bot that periodically watches for new jobs. If it finds a job that is yet to be processed but has passed the run_start_time, run it using exec() or some other command executor. This way the command won't timeout because it is not being run by Apache, but by the cron daemon.
When the command finishes, update the jobs database saying that processing is finished.
From your website, write a frontend that allows the user to see if the requested job is finished yet. Once it finishes, it displays some kind of "Done" indicator or something similar.
Is there a way where I can automatically call a php script after a specified interval.
I have a php script(say remindusers.php) that uses mysql to query a database where people have submitted their weekly reports. This script automatically queries the database and sends an email reminder to people who have not sent in their weekly reports yet.
What I am now supposed to do is give the ADMIN an option to set a reminder start and reminder end date during which calls should be automatically made to my remindusers.php script and cease on reminder end date.
What I learnt from SO/google is that I can setup cron (in LINUX) to automatically call my remindusers.php, but I dont have any shell access to do this.
Else Can I write another php script to essentially sleep every 24 hours and automatically wake up to call my remindusers.php script.
Are there any other built-in methods ?
Any ideas?
Use your site's visitors to trigger the event. Send a message and then check if 1 day elapsed. Then send another. You still need to pay attention not to double/triple/... send deu to synchronization.
When the time has elapsed use a MySQL (or system) MUTEX to ensure only one send occurs.
Yes you can! What you need to do is to use cron jobs. Cron jobs are essentially telling the server to execute a script (PHP or otherwise) at regular intervals. Cron jobs are very powerful and customizable, as you can set virtually any interval for your cron.
If you are using CPanel to manage your site, there is a button in CPanel to view all the cron jobs you have set. There is also a tutorial on that page.
Hope this helps.
Try with this PHPCron
PHPCron is a simple PHP script which lets you run multiple tasks on a schedule or timer. It can be run either from the command-line or via a web browser. Its behaviour is very similar to the popular cron program for UNIX.
http://katyscode.wordpress.com/2006/10/17/phpcron-running-scheduled-tasks-from-php-on-a-web-server/
I understand that you don't have Shell access but have you had a look at the cPanel to see whether there is an option to setup a cron job in there?
I know we have cron jobs in PHP. but I have a project in development phase and we won't have cPanel access.
We have a PHP + MSSQL application that needs to check the database periodically every 1 minute and collect the data and send a mail to a store admininstrator.
How can we do this?
You can have a alternative of cron jobs solution by implement you function in a file (eg: /very/secret.php if your jobs need to be secure, make sure the function can be call only when it get the right parameter eg: /very/secret.php?key=long-random).
Then use some free cron job server on the web like: https://www.easycron.com/ or https://www.setcronjob.com/ (just do a web search for "free online cron jobs"). You give them your URL and some configuration and then your jobs will be executed by them at a specific time of day.
I understand that you need to run a script to check the database periodically every one minute and collect the data and send a mail to store admin. Also I understand that you have a script and you need to run that script every one minute.
It can be done using "cron job", if you are using Linux server. Or if you are using Windows, there is a way to schedule the task (to run that script every min).
Note: it is nothing to with cPanel. Actually, the cPanel provides a user friendly GUI to schedule the cron job.
But if you are not using panel, you can do it manually.
If you are using Linux, here you can see, how to add the cron job - http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
If you are using Windows, here you can see, how to add the cron job - http://windows.microsoft.com/en-us/windows/schedule-task#1TC=windows-7
Is that possible?I know how to refresh a page with header commands or javascript but this demands that the user will constantly have a browser opened.I want to build an automated bot that will not need my computer opened all the time.
Run the PHP script from a command line with your CRON or Windows Task Scheduler. Create a new Task, and execute that task as often as you like without a user or a browser.
C:\php\php.exe -f C:\My_Folder\My_Script.php
So you want to execute your script independently of web-calls, the usual approach would be to launch your PHP-Script using cron.
You can also use 3d party service such as https://www.setcronjob.com/ as "external" cron - if the intervals are not too narrow...
I have a PHP script that processes my email subscriptions.
It does something like:
foreach email to be sent:
mailer->send-email
print "Email sent to whoever."
I'm now encountering rate-limiting by my web host. The mailing library has a built in throttler that will sleep to ensure I stay under the rate. However, this could result in the web page taken multiple hours to actually load.
Will the client side browser ever give up on the page loading? Any suggested better solutions to this?
Why is this being done on a webpage load? This should be an off-line back-end process which is scheduled to run. (Look into cron for scheduling tasks.)
Any long running process should be delegated to a back-end service to handle that process. Application interfaces (such as a web page) should respond back to the user as quickly as possible instead of forcing the user to wait (for upwards of an hour?) for a response.
The application can track progress, usually by means of some shared data source (a simple database, for example), of the back-end process and present that progress to the user. That's fine. But the process itself should happen outside of the application.
For example, at a high level...
Have a PHP script scheduled to run to process the emails.
When the script starts, save a record to a database indicating that it's started.
Each time the script reaches a milestone of some kind, update the database record to indicate this.
When the script finishes, update the database record to indicate this.
Have a web application which checks for that database record and shows the user the current status of the back-end process.
You may not care, but even if you coerce this script into staying alive, you shouldn't purposely run a long running script through the webserver. Webserver's use resource heavy threads or processes to run your script, and they have a finite amount of them available to server web requests. A long running script basically takes one of them out of the pool of processes that can be used to server web visitors.
Instead, use a cron job which executes the php binary directly. Specifically, do not use wget or lynx or any other web browser like program as part of the cron job, because those methods run the script through the webserver. The cron command should include something like
php /full/path/to/the/script.php