Executing External URLs via Cron: The curl Approach
You cannot execute a raw URL directly within a cron job entry. Cron jobs operate in a shell environment that expects commands, scripts, or binaries, not HTTP requests. Your attempt to schedule */5 * * * * /home/test/check.php runs the PHP file if it exists locally as an executable, which is distinct from hitting the public endpoint http://example.com/check/.
To trigger an external URL every five minutes, you must use a command-line tool capable of making HTTP requests. The standard utility for this in most Linux distributions is curl.
The Correct Cron Syntax
You need to modify your crontab entry to invoke the curl binary followed by the specific URL and any necessary flags. Assuming curl is installed on your server, the command looks like this:
*/5 * * * * curl http://example.com/check/ >> /var/log/cron_check.log 2>&1
This schedule tells the system to run the command every five minutes (*/5) across all hours of the day. The curl command sends an HTTP GET request to the specified address. Adding >> /var/log/cron_check.log 2>&1 redirects standard output and errors to a log file, which is critical for debugging if the script fails silently.
Handling Authentication and Headers
In many real-world scenarios, the target URL requires authentication or specific headers that are not sent by default. If your endpoint at http://example.com/check/ requires a cookie session or basic authentication, you must append these to the curl command before scheduling it.
For Basic Auth, use -u username:password:
*/5 * * * * curl -u admin:secret http://example.com/check/ >> /var/log/cron_check.log 2>&1
If the endpoint requires a specific User-Agent header to avoid being blocked by firewall rules or rate limiting, add the -H flag:
*/5 * * * * curl -H "User-Agent: MyCronBot/1.0" http://example.com/check/ >> /var/log/cron_check.log 2>&1
The PHP Alternative
If your goal is specifically to run a PHP script associated with that URL, there are two primary approaches depending on your architecture. If the URL maps directly to check.php located in the web root (e.g., /home/test/check.php), you can simply call that file directly, provided the cron daemon's user has write permissions and the necessary environment variables (like PATH) are set correctly.
However, calling the script by its relative path often fails because cron runs with a minimal environment. You should explicitly invoke the PHP binary or use php-cli:
*/5 * * * * /usr/bin/php /home/test/check.php >> /var/log/cron_check.log 2>&1
This ensures the script executes within the CLI context rather than trying to load through the web server's SAPI, which can lead to missing dependencies or permission issues.
Troubleshooting Cron Context
A common pitfall when moving from a browser request to a cron job is the absence of environment variables. When you visit http://example.com/check/ via a browser, your session contains cookies and headers populated by the frontend. A cron job runs as a system service (usually root or a specific user like www-data) with no browser context.
If your script relies on server-side state managed in a database, ensure that connection strings are accessible to the cron user. If the script expects configuration from .env files located in the web root, remember that web servers often load these differently than CLI tools. You may need to explicitly source environment variables within the cron command or create a wrapper script:
# Create /home/test/check-cron.sh
#!/bin/bash
source /home/test/.env
curl http://example.com/check/
Make the wrapper executable (chmod +x) and schedule the wrapper instead. This isolates environment setup from the cron syntax itself.
Why wget Might Be an Alternative
While curl is the industry standard, wget offers a similar capability for scheduling tasks. Some system administrators prefer wget for its simplicity in handling redirects or downloading output to files. If you switch to wget, the syntax changes slightly:
*/5 * * * * wget -q --user-agent="MyCronBot/1.0" http://example.com/check/ -O /dev/null
The -O /dev/null flag ensures no file is created on disk, keeping the job clean while still verifying connectivity and triggering backend logic if the script handles the request internally.
Ultimately, the choice between curl, wget, or direct PHP execution depends on how your application architecture separates web traffic from background tasks. If you are building a robust Laravel application, consider leveraging queue jobs via Redis or database drivers instead of cron loops for heavy lifting, as this decouples execution from wall-clock time and improves reliability. For lightweight checks every few minutes, curl remains the most direct solution.
