Jwest100 Posted February 8, 2017 Share Posted February 8, 2017 In my php script... I display a form for a specific interval. I have a variable: $auction_end, which is formatted as a unix time stamp, as the end time for that interval. In the script, the first time it is loaded in the browser, I want to create a cron job that visits the URL for the form at the time which is $auction_end. That will trigger an email function within my script. I can take care of making sure the cron job is only created one time with conditional statements. So no worries there. Likewise for not triggering the email function more than once after the end time. The problem then is how do I create the cron job using php code? I'm thinking exec( shell script code); But I don't know the shell commands at all, nor how to use the variable in the shell command. Any and all help would be greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/303131-create-cron-job-in-php-script/ Share on other sites More sharing options...
ginerjm Posted February 8, 2017 Share Posted February 8, 2017 Why do you need a cron job for this task? Normally a cron job is created to perform a task on a regular basis, not on a signal from a periodic script execution. Perhaps you need to re-think what you need to do. It's all very confusing to me right now. Quote Link to comment https://forums.phpfreaks.com/topic/303131-create-cron-job-in-php-script/#findComment-1542444 Share on other sites More sharing options...
kicken Posted February 8, 2017 Share Posted February 8, 2017 Rather than try to dynamically create/remove cron jobs, just create a single job that runs on a specific interval and checks a database for work. Store your $auction_end time and whatever other data is need into your database so that when the job runs it can do what it needs to do. Quote Link to comment https://forums.phpfreaks.com/topic/303131-create-cron-job-in-php-script/#findComment-1542447 Share on other sites More sharing options...
Jwest100 Posted February 8, 2017 Author Share Posted February 8, 2017 Okay... here's the process: The page runs for 24 hours. All of that is already taken care of so no reason to discuss that here. I just need to have the server visit the page URL one time after the $auction_end time of that page. This will trigger an email function which needs to run AFTER $auction_end. I can't depend on a random user to visit the site for me to trigger the email function. So, to make it automatic: -When the page is loaded the first time, create a cron job programmatically using php exec() to visit the page at whatever time is assigned to $auction_end. -After the visit takes place, delete the cron job. Later, perhaps a week later, I may run the page again for 24 hours with different time criteria. Thus the need to make the cron creation/ deletion self contained within the page code. In summary: I just need to run the php exec() function, with the proper command between the (), to create the cron job to visit http://my URL at precisely $auction_end. Then run another exec() to delete the cron job AFTER that has happened. In both cases I'll put a piece of data in the db indicating they took place. I'll check that data on subsequent page visits by random visitors to prevent it from happening more than once. Make sense? Quote Link to comment https://forums.phpfreaks.com/topic/303131-create-cron-job-in-php-script/#findComment-1542448 Share on other sites More sharing options...
ginerjm Posted February 8, 2017 Share Posted February 8, 2017 I think you need to explain yourself further. Or use better language to describe it. You have a "page" that runs for 24 hours? What do you define a "page" as? I define it as the image that the client is showing to the user. It can sit there doing nothing for 24 hours or more, but it is no running unless you have some JS code executing on an interval basis, certainly not on a constant one. You want the server to visit "the page URL"? Not at all sure what that means. Isn't the page url the thing that presented that "page" to the user/client, ie, a script? Although if you want the server to do something related to something on this "page", then I can see how it could be a completely separate cron task. This sounds like what Kicken is telling you to implement. Think about what he said again. If your "page URL" posted a time value to a table, your cron job could execute every 1 minute or every 5 minutes and look at this table for any records having a time value that has expired and do your bidding. And if anyone does stumble upon your waiting "page" that could also trigger a record in the table telling your cron job something else. Quote Link to comment https://forums.phpfreaks.com/topic/303131-create-cron-job-in-php-script/#findComment-1542473 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.