Auto-Publishing AI News Without Server Crashes: My Simple PHP Cron Fix

发布于 2026-07-25 23:46:09

Running a Smart Automated News Site Without Breaking Your Web Host


I recently audited an automated news site for a client. The site was built to generate articles using AI to fill up its daily magazine layout. The owner was frustrated because the homepage kept timing out and showing "504 Gateway Timeout" errors to visitors.

When I checked their server logs, I found the problem immediately. Every time a reader loaded the homepage, the website ran a slow, live script to call an external AI API to fetch new articles.

Because the AI takes several seconds to write and return the text, the visitor's browser had to wait. If ten people visited at the exact same time, the server ran out of PHP processes and crashed.

Here is how we moved these heavy API calls to the background and made the website load instantly.

Step 1: Use Asynchronous Background Workers

Never make your website visitors wait for an API call. Instead, your server should fetch the news in the background and save it to your database. When a visitor lands on your site, they should only load fast, static data from your database.

To make these background calls safely in PHP, I write scripts that utilize cURL options to prevent the script from waiting forever if the API gets stuck. If you are coding this yourself, you can read the official PHP cURL manual page to see how to set strict execution timeouts.

A basic cURL timeout configuration looks like this:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.openai.com/v1/chat/completions");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // Stop waiting after 10 seconds
$response = curl_exec($ch);
curl_close($ch);

Step 2: Set Up an Automated Cron Job

Once your PHP background script is ready, you need to tell your server to run it automatically. We set up a system cron job to run our generator script once every hour, rather than on every page load.

To do this, log into your server via SSH and edit your cron table:

crontab -e

Add this line to run the background script every hour:

0 * * * * /usr/bin/php /var/www/html/scripts/fetch_ai_news.php > /dev/null 2>&1

Now, the server generates the fresh articles quietly in the background, and your visitors get a lightning-fast experience.

Choosing the Right Foundation

If you are trying to write an entire AI-powered magazine portal from scratch, you will spend months debugging these kinds of API bottlenecks. Using a professionally written, pre-built framework is a much smarter move.

For this client, we ended up migrating their system over to InfyNews : AI Powered Articles & Magazine Script which we sourced from GPLPAL. It already has background scheduling built into its core design. It handles the API limits, queues up your requests, and stores the completed articles safely in your database so your public pages never slow down.

For web developers looking for other modular options to build custom magazine dashboards or auto-posting networks, browsing through a trustworthy PHP Scripts download directory is always a great place to find secure, tested backend templates.

Summary Tips for News Sites

If you are running an automated news site, keep these rules in mind:

  • Never run API calls live on page loads.
  • Always set short timeouts on external requests so your server doesn't get stuck waiting.
  • Use database caching for your most popular category pages.
0 条评论

发布
问题