Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've looked into this, and I've found out about Cron jobs - but the thing is that it only has to run once ever.
Background:
My users can create something that expires - and they choose when it expires.
One of my solutions: Maybe I don't have to expire it on that day - whenever someone requests that thing, then I can check whether or not it has already expired, and if it has, then I can change that thing accordingly.
Why it wouldn't work: I'd also like to send an email out when that thing expires.
Thanks for any help!
You could save a time-stamp together with the user item, that you call expire for example
then you could run a cron job that removes all items that has passed the expired time.
if($item.expire < time())
{
// Item has expired, remove it
}
You can set up a cron job that runs a script which compares the date with the expriation date. If It is equal to the expiration date, send a mail, else do nothing.
If you re on MYSQL you could use events. I ve used them successfully on expiring sessions.
You could choose to have kind of "runner" that checks for available jobs to run at the specific time and runs them.
Then you can create a cronjob that fires the runner.
Cron jobs are what you're looking for, you just have to find a way to run the task only once.
https://stackoverflow.com/a/5473841/2055152
Try this out to execute a command on 30th March 2011 at midnight:
0 0 30 3 ? 2011 /command
This answer offers a simple solution. If you want users to create their own events with PHP, just convert their desired date to the cron format. Just be careful on what you allow them to launch!
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a table, that consists of all the info to run a campaign. The info includes, the time interval to check the campaign (10 mins, 15 mins etc.) and other information to check whether it meets the specific requirement or not, to run the campaign.
At the moment, what I am planning to do is:
Add my code in one php file
In the code, go through all the rows of the table
Check if it's the time to check the campaign or not (via the interval)
If it's the time to check the campaign, then go through other details of the table and based on the set conditions, send an email or SMS.
I am planning to run a cron job which goes through this php file, after every 10 minutes (As it's the shortest check interval)
I need suggestions, whether it's the proper solution or not OR if someone has any better and efficient solution?
That's a decent starting point...
If it's the time to check the campaign...
Keep in mind that sometimes a cron process takes longer than expected, gets stuck or your system crashes in the middle. Ideally your process will keep track of a.) what it's doing, and b.) when it did it. And be able to fix problems like skipped or stuck processing.
It could be that you never want to send a message that late. Then again you may want to make sure all of the missed messages get sent. Your code should be able to handle this case automatically to some degree. Maybe automatically do anything that's should've been done in the last hour but wasn't and ignore anything older than that. For older stuff you'd have to manually run the script. Make sure your script has command line arguments that simplify you forcing it to run for prior time intervals and specific campaign IDs. This will make your life way easier after a disaster.
I suggest that you have some kind of reporting so you can keep track of your processing in real time. Pretty simple if you're writing state info to your database. Add on an end of processing timestamp and you can even see how long your cron jobs are running. If you don't want to use this state info in your cron job you can just write it to a log file instead of a database. And in that case (if needed) you would use a lock file to indicate when a cron job is running and prevent other cron jobs from starting at the same time. Regardless, it's good practice to write a log file so you have a record of what happened. Imagine if your cron job sent an email but crashed while attempting to write the state to the database. You'd at least have a log line to help you investigate later.
I am planning to run a cron job which goes through this php file,
after every 10 minutes (As it's the shortest check interval)
So the speed of your script will vary with the amount of data, latency of external services (assuming your script talks directly to such services). I would start with a much longer cron job start interval - assuming that your client/use case allows for that. If you follow the suggestion above to have your script automatically handle skipped times this isn't a problem. The more stuff you're processing the more time your script will eventually need. So on day 1 it might only need 1 second. But on day 300 it might need 15 minutes? (At that point you could decide that you want to have multiple processes/threads running at the same time with each one focused on a single campaign or range of campaigns. Who knows...) But you'll know because you have reports/alerts/logs on the start/end processing times.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a php file called reportemail.php file, And I have one drop down with 3 values like daily,weekly,monthly
If the client set time as daily I want to run the file(reportemail) and I want to send email every morning..and I want to do this is in localhost only,not in server.and I want this should be happen if the system is in shutdown mode also.
I already tried with cron.bat file it is working fine,but there manually I gave time..instead of giving manually I want to give dynamically.
Here is how I did in cron.bat file
E:\xampp\php\php.exe -f E:xampp/htdocs/custom/reportemail.php
And in reportemail.php file i have my email code and script code like this
<script>
setTimeout(function () {
window.location.reload();
}, 5 * 60 * 1000);
document.write(new Date());
</script>
Using cron is still the way to go here. You just want to separate the scheduler from the scheduled events a little bit more.
Don't try to have the users create/modify the cron job scheduling itself. Instead, schedule a universal cron job to execute at the narrowest time frame (daily). That single job would:
Check for user reports scheduled to run.
Check when those reports were last successfully run and if it's time to do them again.
Run the reports.
So instead of scheduling cron jobs, your users are simply saving a record to a database indicating when to run their reports. For example, consider a table like this:
UserReports
----------
ID (PK)
UserID (which user)
ReportID (which report to run)
ScheduleID (which frequency - daily, weekly, etc.)
LastRunDate (date of the last successful run)
Any time the cron job executes, it would select from this table any records which meet the criteria:
ScheduleID is daily and LastRunDate is 1+ days ago, or
ScheduleID is weekly and LastRunDate is 7+ days ago, or
ScheduleID is monthly and LastRunDate is 1+ month ago
Loop through the records which meet those criteria, run the reports, update the records with a new LastRunDate of today.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The title might be a bit unclear. But I have this problem with my website. I have some data stored in a sql database, including a timestamp. But now I need to make some calculations with that data and send an email, after a certain duration after that timestamp. But how do I do this? Since my website and my database only contain static data...
So to be clear: I have a site on shared hosting with a database. In that database are items that all contain a timestamp value. Now I need the server to undertake an action for every item in the database on a certain duration after that item's timestamp, for example send a mail.
This is probably an easy question, but I have no clue what to look for...
EDIT: I think I have found a solution by using setcronjob.com , thanks!
You could run a job every so often to figure out how old that timestamp is and then send the emails you need to send. But how to run the job?
If you can run scheduled jobs on your hosting (ie, you own the machine/VM and are not on shared hosting) you can run these jobs every x days/hours/minutes/seconds. In Linux, look up cron jobs, and Windows look up Task Scheduler (I think)
If you're on shared hosting it's more difficult. Maybe you can convert some of your static pages into dynamic pages that run these jobs? Depending on how long they take to run you might want to look at running these jobs in a new thread (pthreads). You'll need to have pthreads installed for this on your hosting, so #1 - scheduled jobs - is definitely more preferable.
You probably want to use a Cron task to search your database for every row with the timestamp near the current time. From that point, you can simply do your calculations and send the email.
Could you tell me more about that "certain duration after that timestamp"?
Also, in the database you could setup a job that runs on a schedule to make the calculations and email the results.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
(PHP)
So here is what I would like to achieve.. but apparently I can't find any relevant information anywhere.
I would like my site to select a random value from my database every 5 minutes (for example: an ID from a table), and everyone that visit my site would see that same selected value, until the "server" randomly select another value 5 minutes after the last select.
So, I guess there would be a function that do the select from the database,
but
1) where would I implement that function?
2) where would I call it? I'm not a expert in PHP, but I don't think it can be called from the client, else every client would call the function and they would not see the same value as others?
3) how do I to set a 5 minutes timer to call the function again?
In brief.
Server select a random value (ex: an ID from a table).
It get displayed on the site (same value from everyone for 5 minutes)
5 minutes after last select, another value is randomly selected
It get displayed..
Repeat...
I will probably use Laravel to created my website, I'm saying just in case it's important for the solution.
Thank you for your help!!
1) As far as implementing functions goes, It is pretty simple. If you need to use your function in multiple PHP documents, place int in a separate file and use an include to bring it into the document(that the client calls):
<?php include_once($_SERVER['DOCUMENT_ROOT']."/Path/To/file/phpscript.php");
If you only need it in one document, simple place it in in the document(that the client calls) before you use it.
2 & 3) There are "cron jobs" if you'r using Linux(I personally don't like using them), which could be used to awaken the php script every 5 minuets(in which case keep the function in its own file), but instead I would (edit: not, use cron jobs, this is a last resort)recommend another method instead. In your function(which will run when the client requests it), write the time you last requested it in to some text file, database or anything else. when the function runs, check if the written time was more than 5 minuets ago. If it was, create a new number and write a new time down. If it wasn't, fetch the old number from the database.
EDIT: Use Cron Jobs
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am building a web application that would requires me to reserve a seat for the customer for 30 minutes or closing of the browser before the reservation gets deleted. Any suggestion of the best way to do this?
I thought of adding a record into MYSQL and runs a cron job but that would not give the users the most updated results. Any advises?
Thanks!
Write a record with an expiration date. Use that expiration date when writing queries. Clean up expired records every so often.
Closing the browser however is more complicated and not reliable. Connections to the server in http are stateless and as such you really wont know when the browser has been closed. You can regularly poll with ajax and delete records when there are no longer any poll updates.
This problem is actually 2 problems:
How do you clean up old records
and
How do you ignore expired records.
Add an expire_time field to your reservations table. You probably have some logic that looks at how many seats are still available, add the expire_time to that logic so that expired records are ignored.
Then write a script or function that gets called periodically (cron works, so does register_shutdown_handler()) to clean up the expired records.
This way, should your cron ever fail or be slow for some reason, you're not accidentally blocking new reservations. You could even skip the cleanup step completely and take the (minor) performance hit.
I believe it should be resolved by session, not the mysql.
But if you insist, you should update item every time page get hits, and you can do also some sweepups
EDIT
Yes, of course. The reservation system must be multi user. If one user does the reservation than the other user must not be able to get the seat.
Yes, simple resolution is just to add there new column called reserved that will specify the time until it is reserved.
Why do you need cron for it?
BTW: sessions are stored on server not client. cookies are stored in client not server.