If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Achieving Hourly PHP Script Run with No Cron

Started by Buffalos, Jul 04, 2023, 12:07 AM

Previous topic - Next topic

BuffalosTopic starter

How can I implement running the php script every hour if there is no cron on the hosting?
  •  


microsoftcygnet

1) Utilize hosting alongside a cron scheduler.
2) Occasionally, services are scheduled to arrive at specific locations similar to an external cron job.
3) Employ user visits as a stimulus to initiate specific scripts; needless to say, precision in execution is crucial.
  •  

Optimitron

During the era of dinosaurs, they utilized a peculiar approach: they would record a timestamp in the database, namely time() + 3600, and subsequently establish a condition.

if (time() >= timestamp in DB) // at this point, the script gets executed and updates the timestamp in the database again

Although unconventional, it was one of the options available to them.
  •  

sagetechnology

You can execute it through php-cli or shell_exec(). At the end of the shell command, include the ampersand & symbol. This will allow the script to run in the background.

This method was particularly useful for me when I needed to start long-term processes without waiting for them to complete.
  •  

Dcliniq

If your hosting does not support cron jobs, you can try implementing a workaround using JavaScript. Here's an example of how you can achieve it:

1. Create a JavaScript file, let's call it `scheduler.js`, and add the following code:

```javascript
function runPhpScript() {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "path/to/your/php/script.php", true);
    xhr.send();
}

runPhpScript(); // Call the function immediately

var interval = setInterval(runPhpScript, 60 * 60 * 1000); // Run the function every hour
```

2. Upload the `scheduler.js` file to your hosting server.

3. Include the script in your HTML file, like this:

```html
<script src="path/to/scheduler.js"></script>
```

4. Replace `"path/to/your/php/script.php"` with the actual path to your PHP script. This path should be accessible from the web.

With this setup, the JavaScript code will run your PHP script immediately when the page loads and will continue to run it every hour thereafter. Keep in mind that if you have a low-traffic website and the page is not visited frequently, the PHP script may not execute exactly every hour. Some requests may be missed, resulting in a delay in execution.

If your hosting environment does not allow you to run cron jobs or execute scripts at specific intervals, you can explore alternative solutions. Here are a few other options you can consider:

1. External Cron Service: Use an external cron service that allows you to schedule and trigger HTTP requests at specific intervals. These services act as cron job providers and call the URL of your PHP script at the desired times. Some popular external cron services include EasyCron, Cron-Job.org, and SetCronJob. You can sign up for these services, configure the schedule, and provide the URL of your PHP script to be executed.

2. Ping Services: You can utilize ping services like Pingdom or UptimeRobot to monitor your website. These services periodically send HTTP requests to your site to check if it is running correctly. Instead of monitoring your site for uptime, you can configure them to call the URL of your PHP script at regular intervals to effectively achieve the desired scheduling.

3. Webhook-based Solutions: If you have control over another server or service that supports scheduled tasks or webhooks, you can schedule a task on that server to trigger the PHP script on your hosting. You can then configure your hosting to expose a webhook endpoint and have the remote server make an API call to that endpoint at the desired intervals.
  •  

yptogvij766

Use external cron services like EasyCron or Cron-job.org to ping your script URL every hour, offloading scheduling to a reliable SaaS. Another slick method is setting up a serverless function (AWS Lambda, Cloudflare Workers) to hit your script endpoint on schedule, decoupling from your hosting limits.
Lastly, if you're stuck on cheap shared hosting with no cron and no external calls allowed, consider embedding a long-running PHP daemon via CLI (if SSH access exists).
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...