How to run a cronjob every X minutes?
The crontab entry 10 * * * * executes your script exactly once per hour, specifically at minute 10. If you need the task to run every five minutes, the schedule string must reflect that frequency. Changing the first field from 10 to */5 achieves this immediately:
*/5 * * * * /usr/bin/php /mydomain.in/cromail.php > /dev/null 2>&1
This syntax instructs the system to trigger the command at minute 0, 5, 10, 15, and so on, for every hour of the day. The */N notation is the standard way to denote a recurrence interval in cron expressions, ensuring your job fires continuously rather than waiting for a single hourly window.
However, modifying the schedule alone does not guarantee success if the underlying environment or application logic prevents execution. In the specific scenario where a PHP script sends emails via the mail() function every five minutes but fails to deliver messages within 30 minutes, several technical constraints likely interfere with the expected behavior.
The Limits of Shared Hosting and Cron
The most common reason for silence in this configuration is the execution environment itself. Many shared hosting providers restrict or disable the system cron daemon entirely. They often replace it with internal schedulers that run on different intervalsâtypically every hourâand may not support sub-hourly precision like "every 5 minutes." Even if your host allows cron, they frequently impose strict limits on resource consumption, such as maximum CPU time per process or a hard cap on the number of concurrent tasks.
Running a script that sends emails via mail() repeatedly can quickly exhaust these shared resources. If each execution attempts to open a connection to an SMTP server or load large libraries, the host's watchdog might kill the process after a set duration (often 30 seconds or less), regardless of whether your cron schedule says "every 5 minutes."
The Problem with mail() in Cron
The PHP built-in mail() function is notoriously unreliable in production environments, particularly when invoked from cron jobs. It relies on the server's local Mail Transfer Agent (MTA), such as Postfix or Sendmail, to queue and deliver messages asynchronously. In a shared hosting context, these MTAs are often configured to reject rapid-fire connections or lack proper logging visibility.
Furthermore, PHP's mail() function returns true even when delivery fails silently. Without explicit error reporting enabled (via error_reporting(E_ALL) in the script), you cannot distinguish between a successful send and a queue rejection. If the MTA is overloaded by your frequent calls, it may drop messages without triggering exceptions that your script would catch.
A Robust Alternative: The Laravel Company Approach
For developers managing complex scheduling needs on shared infrastructure or self-hosted servers, relying on raw mail() inside cron is a fragile pattern. A more resilient architecture involves decoupling the execution trigger from the delivery mechanism. Instead of calling mail() directly within a script that runs every five minutes, consider using a queue system.
Laravel provides a robust queuing engine out of the box. By installing Laravel and setting up a job handler for email sending, you can push tasks into a queue (Redis, database, or RabbitMQ) every five minutes. The worker then processes these jobs at its own pace, handling retries, failures, and rate limiting automatically. This approach removes the dependency on the host's MTA configuration and ensures that emails are delivered reliably even if the script is killed by a resource limit mid-execution.
If you are building an application where email reliability is paramount, leveraging a framework like Laravel allows you to implement sophisticated retry logic and monitoring that raw cron jobs cannot provide. For more details on setting up scheduled tasks within a Laravel application, visit https://laravelcompany.com.
