Jump to content

Recommended Posts

Hi guys,

 

I want to run several scripts between hours a and b daily between c and d number of times. I also want it to run on different times daily, so a static cron that runs daily is not an option.

 

The solution I made up for this is the following (I'd like feedback both on the method used and possibly help on a little problem I ran into on the way):

 

Example: run script test.php between 10AM and 2PM daily between 2 and 3 times a day.

 

I have a table called 'DB_Cron' with following fields: ID (so I can link to other tables in my db)[int(10)], BeginTime[int(2)], EndTime[int(2)], NextCron[datetime], CronLog[text].

 

BeginTime and EndTime are my 10AM and 2PM, they are static.

 

I would make 2 php files:

 

cronreset.php and cronrun.php

 

Cronreset.php runs daily at 0:00 and it creates between 2 and 3 different timestamps (depending on values in another table), it stores these in CronLog seperated by a comma and sorted on time, earliest first. The earliest he stores in NextCron.

 

Cronrun.php is set on a 5 minute cron. Everytime it runs it checks if another 'NextCron' has 'expired'. If it has, it runs script test.php. At the end of the file it resets NextCron, grabs the next datetime from CronLog and populates NextCron with it.

--

 

I hope all of that made some sense. My first question would be: is there a better solution for this?

 

And second question is more of a direct php question: how can I sort the number of different datetime stamps I created in an array so that the earliest is the first one in CronLog?

 

All help is appreciated, if anything is unclear, I'll do my best to clear it up.

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/163764-dynamic-custom-cron-php-script/
Share on other sites

I thought about that as well, but since I can't possibly run a homeserver, I depend on external hosting at GoDaddy.com (I'm not sure if that's considered advertising or illegal around here, if it is, I'll delete the hosting company but it's relevant to the rest of my answer). Creating a new crontab daily would mean I'd have to write a script that logs into their hosting pannel and create the crontab there. Which is way more complicated than doing it like this.

 

Thanks for the tip and reading my question though :)

I've looked into that and it would really be very tough with all the security protocols, I also think it would be considered illegal by the host and I don't have shell access to input it directly into linux.

 

Any more ideas are certainly welcome or an answer to the second sorting question.

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.