Jump to content

create cron job in php script?


Jwest100

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.