0

How can i run or refresh PHP page every 10 Minutes or an hour. so that add new record from one database to another i doesn't matter with using javascript but without using cron jobs.

Hulu
  • 31
  • 1
  • 8

2 Answers2

1

You can depend on the traffic to your website if it's frequent enough. Drupal has something called poor man's cron. Basically you store the date-time that the cron job was last run. Each time a user visits a page on your website, you fetch the date-time, compare it with the current date-time to see if you need to run the cron job (in your case, see if an hour has passed). If the required amount of time has passed, then run your cron job, and store a new date-time. If your required amount of time hasn't passed, then do nothing.

This approach has a few caveats.

  • You really should be using cron, and not doing this. There's a reason it's called poor man's cron. If you're using some hosting service by someone else, their technical support should be able to help you setup a cron job. If the service has cPanel you can do it from cPanel. https://documentation.cpanel.net/display/ALD/Cron+Jobs
  • You are completely dependent on website traffic to trigger your cron job. You need to decide if it's OKay for your cron job to not be run for a period of time where users are not visiting your website.
  • You're slowing every request to your website down by a fetch to whatever persistent store is holding the last date-time the cron job was run.
Jazzepi
  • 5,259
  • 11
  • 55
  • 81
  • Yeah, poor mans cron is a pretty bad solution, for the reasons you describe. If you don't want to be dependant on trafic you could send some artificial trafic from a free host somewhere (heroku, aws, etc.), but still, such hax! – jjm Nov 30 '14 at 05:06
0

with javascript you can use the setTimeout() function but the user will need to keep the page opened.

i.e

setTimeout(function(){ your_process(); }, 360000); // every hour
joseluiscc
  • 234
  • 2
  • 5
  • hummm but that is what i want to avoid i want to run it as a background any option? – Hulu Nov 30 '14 at 04:49
  • Why are cron jobs not an option? – jjm Nov 30 '14 at 04:51
  • @jjm contacted my website hosting provider and told me that cron runs only once per day for 20 sec and but the process may take more than that.is it possible to use outside cron service? how much does it cost me? – Hulu Nov 30 '14 at 04:55
  • If you just need some traffic on a regular basis, put a cron job on an aws micro (free) and have it ping your website regularly. Or think about migrating to AWS, rackspace, or some other "here is a machine, do whatever you want on it" provider altogether. It's pretty cheap and you can just do anything you want on the box. – jjm Nov 30 '14 at 05:04