简体   繁体   中英

Can we schedule cron job in the php script?

I'm new to this cronjobs and I want few emails (2k to 3k mails to be more precise) to be sent at specific time and date which are in the database table.Currently to achieve this, I'm calling my mail function file(sendmail.php) for every minute using cron job and comparing the current time and the time which comes from the db table, if true the mail will be sent.By doing this I'm afraid there will be some effect on the performance.

Can we schedule cronjob right after the insert query in the php script. So that I can pass those time and date variables to it?

Does calling the file for every minute in the cron job is a good practice? Will the performance get effected because my application will be used by 25 users at a time?

Although by calling the file for every minute achieves my task, but still want to know if there are any better ways.

Thank you in advance.

for every minute using cron

If you're firing off cron jobs every minute then you're doing some thing wrong. There are problems with jitter, and concurrency.

comparing the current time and the time which comes from the db table

Does that mean you are doing the tie check outside of the DBMS? That would be very silly.

Can we schedule cronjob right after the insert query in the php script

Yes, although you'd need to use sudo to create privilege separation. However you having (potentially) thousands of cron jobs is a very bad idea.

While there is a lot missing from your problem statement, based on what you have said, I'd suggest having a cron job running once every (say) 15 minutes, polling the database for the emails to be sent in that time window - with the time comparison and concurrency locking done in the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM