Execute a timed command in a time command - php

I am working with an php application where i need to check a database of users for whom has birthday today.
This i do through a cron job.
Now, when i get the users with birthday, i need to send them a SMS.
The SMS has an individual time associated which determins when the SMS should be send.
Lets say John and Peter has birthday today. Peter works from 01-09 so he should get the SMS at 01 and Peter works from 08-16 so he should get his SMS at 08.
I was thinking about dynamically creating the same amount of cron jbos which equals the people who has birthday on a given date. Those cron jobs would then send the SMS to the appropiate people this one time and thats it.
My question is, is there a smarter way to do this?
Secondly, if the cron job generation idea is good enough, is there a way to remove individual old cron jobs so i dont clug up the cron job list with old jobs which will never be run again?
Thanks in advance.

If you would like to send messages to each person at a specific time using cronjobs then you would have to create a cronjob for each person. Very bad idea in my eyes as you could end up with over 1000 cronjobs!
You would be much better with a system that runs a cronjob every 5 minutes or so (however often you want), and this cronjob executes a PHP script to determine if there are any messages to send out at that time, or between now and the last cronjob.
Whilst this may mean your users could get their message up to 5 minutes late, it is unlikely because nobody I know starts work at 8:02, and therefore, if the cronjob is at 8:00, 8:05, 8:10 etc, you will almost always get the message sent right on time!

If you're worried about creating too many cron jobs (not sure how many would be a problem on your system), then why not create just one cron job at the OS level? Then you can call your program every 10 minutes or so, and it can check if there are new people that need to be sent an SMS.

Related

Run laravel jobs one time at specific time only

I came across a situation i want to trigger some code at specific time, i.e when user does booking, the freelancer must accept/reject the booking request, if he doesnt, after x duration (15* mins lets say) it would be rejected and user would get push notification. All code is done but currently im running a cronjob after each 1 minute which checks for any unresponded bookings and checks when their time (15mins, dynamic) passed so then I execute my code for afterward, it is not good i guess as its running db queuries over and over each minute.
I'm aware with laravel queue jobs as well but didnt see anything for me to run that job for a specific time only (i.e execute this job after 15mins, if it isnt responded, reject it)
have you looked at Queue delay?
https://laravel.com/docs/9.x/queues#delayed-dispatching
This sounds like what you are looking for, I would just trigger the queue and delay when they make a booking so it executes 15 minutes after.
Use scheduled tasks.
use App\Console\Commands\SendEmailsCommand;
$schedule->command('emails:send Taylor --force')->daily();
$schedule->command(SendEmailsCommand::class, ['Taylor', '--force'])->daily();
https://laravel.com/docs/9.x/scheduling#scheduling-artisan-commands

Setup reminder system with cron

I have specific dates for users interviews. Here is my table:
interviews
-------------
user_id
interview_date
interview_time
reminder_time
The reminder time can be 1 hour, 5 hours, 1 day, 3 days, 7 days, and 14 days.
How would I go about setting up a cron job or something to check the database and send out a reminder if the interview is coming up?
For example, if the interview is 5 hours away, the database would send an email reminder to that user.
Should I use cron? Daemon? Not familair with daemon, just saw it when I was searching it up. Or should I use something else? This is a pure/native PHP application with no frameworks.
Here is what i have so far:
$check = $db->prepare("SELECT * FROM interviews");
if(interview is coming up) {
sendEmail();
}
but i'm confused as to where and how should I search for these interviews and send out reminders.
If i search for ALL interviews, that is putting a huge load on the server.
thank you for all the help and suggestions!
Unless you want your PHP process to run perpetually you'll need some kind of regular scheduler like cron. This can set your code to run once every minute, every hour, or whatever interval makes sense.
Normally every five or fifteen minutes is sufficient to check for upcoming events and send out notifications for them.
Be sure to set a flag in your database for if something has been notified already so if your script crashes and you need to re-run it can pick up where it left off.

User configurable notification frequency in LAMP Web App

I am developing a Web Application for businesses to track the status of their repairs & part orders that is running on LAMP (Linux Apache MySQL PHP). I just need some input as to how I should go about allowing users to customize the frequency of email notifications.
Currently, I just have a cron job running every Monday at 6:00AM that runs a php script that sends an email to each user of their un-processed jobs. But I would like to give users the flexibility of not only choosing the time they are sent at, but the days of the week as well.
One idea I had was, some way or another, storing their email notification preferences in a MySQL database, and then writing a php script to notify via email but only if the current date/time fits within the criteria they have set & write in code to prevent it from being sent twice within the same cycle. Then I could just run the cron job every minute or 5 or whatever.
Or would it be better to somehow create individual cron jobs for each user programatically via php?
Any input would be greatly appreciated! :)
No you are right.
Individual crons will consume many resources. Imagine 10k of users with a request to send mail at different times ... this imply 10k of tasks.
The best solution is to create a cron task that will run on your users and take the correct actions.
Iterate on your users, check the date/time set up, detect change and send mail with adding a flag somewhere so said "it's done" (an attribute last_cron_scandate or next_calculated_cron_scandate could be a good solution)

PHP run script after few hours

I have a website in PHP and users can schedule message to be sent. I can sent message with command similar to this one:
php sendMsg.php 249
where number is ID of the message
Many people suggested to use cron jobs, but since I don't want to run this in interval cron is no option(only once - for example after 3 hours).
My idea was as follows:
$seconds = $hours*60*60;
exec('sleep '.$seconds.'; php sendMsg.php 249');
But this wont work because it will block php for further executing. What is the simplest way to achieve this?
You said you don't want to use a cron job because you only want the message sent once, but this is mis-understanding the way that a cron job would be written for this kind of task
Consider a situation where you have many users creating many messages to be sent at various given points in time.
You don't want to have a PHP program sitting running on your server all that time for each of those messages; it would be wasteful of server resources, even if they were all just sleep()ing for the duration.
Instead, one would use a cron job to run a short-lived PHP program once every minute (or whatever interval suits you).
Your message creation program would not be written to acually send the message; instead it would insert it into a database, along with the time it needs to be sent.
Meanwhile, the cronjob PHP program would scan this database every minute to see if there are any messages that are due to send but have not yet been sent. It would then send those messages and mark them as 'sent' on the DB.
This is the standard way to write this kind of thing, so it's not surprising that people are recommending it to you.
Doing it this way means that you never have a program running on your system for longer than necessary. Both PHP programs do their job quickly and exit, meaning that no-one is kept waiting for them.
It also makes it much more robust. Imagine if your server had to be rebooted. If you had a bunch of PHP programs running for hours waiting for their moment to send their message, they'd all be lost. On the other hand, if they had saved their message to a DB, the cron job would find them and send them correctly once the server was restarted.
Put the schedule in a database. Run a cronjob every minute or so, check the database if a message should be sent within this minute, and send it.
Is there a reason you don't want to use a cron job? That would be the simplest and most efficient way of sending the messages.
I would think that a cronjob ist still the right way
Create a table where the to be send messages are stored, with a timestamp when to be send and a flag for isSend
Create a cronjob - start php skript every 1 minute , which sends the messages with timestamp < current time and isSend = false
Ignore suggestions of cron, if you want to simply wait a period of time then use the at scheduler:
$hours = 2;
$command = sprintf('echo "php sendMsg.php 249" | at now + %d hours', $hours);
exec($command);

Execute script at variable time

I'm aware of cron jobs to execute commands at a certain time, but what if that time is not constant? For instance, suppose a user asks for a reminder email exactly 1hr after signing up for something, is there an easy way to go about doing this?
Timing is critical. I am actually trying to create AI that will essentially act on its own but only at variable points during the day. Any help would be appreciated!
You can use at to schedule jobs for specific times. cron is for repeating jobs, at is for one-shot/oddball interval ones. Both have a resolution of 1 minute, though, so you can't specify a start period with seconds granularity.
The command's available on both Unix/Linux and Windows.
Here a workable flow:
user Requests email in 1 hour
You insert into the a table action (action_id, time)
On the PHP server create a cron job to check the action in the action table every minute, then do the action that need to be done at that time
That is a simple example from the request. It might get a bit more complex then that.
EDIT : this suggestion might be good only if you need to be very precise with the time management!
if you dont wanna use the cron triggers and you are not comfortable with them here are two php scheduling libraries..
1) http://www.php.brickhost.com/
2) http://www.phpjobscheduler.co.uk/
Try them if you like:

Categories