0

is there any way to execute a php function according to a specific given time without using CRON?

because i'm creating a script which sends notifications to the clients about their sales at a given time gaps.

**Please note : time gaps will be selected by the clients so there should be an option for them to setup the time gap **

ganeendra
  • 1
  • 6
  • 2
    why do you want to avoid use of cron? You are literally describing a cron job ... – treyBake Dec 11 '18 at 11:44
  • @treyBake, i don't mind using cron if there is a way for my clients who buys the script capable of setting up cron job to do following task through my script. is that possible for me to setup cron job according to user's selection programmatically? – ganeendra Dec 11 '18 at 11:51
  • yeah just use their input and append it to crontab ... – treyBake Dec 11 '18 at 11:53
  • 1
    But that looks like you would be creating a cron job for each clients sale. So if 10 clients mad 10 purchases 10x10=100 cron jobs. I think you need to rethink how this notification service should work – RiggsFolly Dec 11 '18 at 11:55
  • But how are you planning to execute your script. What will call it? You can implement some kind of a cron manager. It would be called by cron every minute and can decide which task to call. So this way you can store some client 's configurations in database and use them. – Viktar Pryshchepa Dec 11 '18 at 11:56
  • @treyBake Thanks for your replies but the issue is client who purchases the script will be installing it to their servers. not mine. so how can i do this? is there any easier way? – ganeendra Dec 11 '18 at 11:59
  • if it's on their server, you have no control over their cron jobs, just set up a wiki page for your code that advises how to set cron jobs – treyBake Dec 11 '18 at 12:00
  • @treyBake are there any other solution without using cron ? – ganeendra Dec 11 '18 at 12:02
  • no... not unless your client wants to manually run a script every X minutes ... – treyBake Dec 11 '18 at 12:06
  • So how'd I do this is to create a table where they insert how often they want to be sent the notifications and then you can then add a cron job that checks when the last one was sent and if the time has elapsed to send another one – Paddy Hallihan Dec 11 '18 at 12:47

1 Answers1

1

It could get really messy if you start programmatically setting cron tasks for each user, but I think cron would would be part of the solution.

I think it would be better to store each client's scheduling preferences in a database, then run a script periodically (with cron) to check whether it's time to send their notifications, and then send accordingly.

This would enable them to update their preferences without having to find and update (or remove) individual cron jobs.

tshimkus
  • 1,165
  • 2
  • 16
  • 22
  • Thanks for your replies but the issue is client who purchases the script will be installing it to their servers. not mine. so how can i do this? is there any other easier way? rather than using cron – ganeendra Dec 11 '18 at 12:01
  • if they will install it on theyr own Servers they can Setup the cron by themselfes too ... – mech Dec 11 '18 at 12:07
  • If that's the case you could schedule a cron task programmatically with PHP: https://stackoverflow.com/questions/4421020/use-php-to-create-edit-and-delete-crontab-jobs. If using it on Windows they may need to use the Task Scheduler instead. – tshimkus Dec 11 '18 at 12:08