How to setup a wget cron job command

How to setup a wget cron job command

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why Your Wget Cron Job Appears Silent and Where to Redirect Logs

When attempting to automate a PHP script via wget through a cron job, it is common to encounter behavior that defies visual expectation. You construct a command like /usr/bin/wget -q http://www.domain.com/cron_jobs/job1.php > /dev/null 2>&1, expecting silence if the request succeeds or output if it fails. However, you might observe that while your terminal shows "No output generated," an empty file appears in /root on every execution. This discrepancy between the command line and the filesystem often stems from a fundamental misunderstanding of how shell redirection works within cron environments versus interactive shells.

The core issue lies in where the standard input, standard output, and standard error streams are being sent. In your specific example, you explicitly redirect standard output to /dev/null using > /dev/null. You then redirect standard error to wherever standard output currently points with 2>&1. Since standard output has already been discarded, standard error is also discarded. This results in a truly silent process regarding the HTTP request itself.

However, the creation of an empty file in /root suggests that something else is writing to that location. If you are running this as root, any script execution might inadvertently touch files owned by the current user context if permissions allow, or more likely, there is a misunderstanding about what is being captured. If you intended to capture the output into a log file named log.txt in /root, your command should look different. To capture both stdout and stderr into a specific file, you must redirect standard error before discarding it:

/usr/bin/wget -q http://www.domain.com/cron_jobs/job1.php > /root/log.txt 2>&1

In this corrected syntax, > /root/log.txt opens the log file and redirects stdout there. The 2>&1 then takes the error stream and points it to the same destination (the now-open file handle). If you omit the initial redirection to the file and simply use 2>&1 without a preceding >, nothing gets written to the file because standard output has nowhere to go yet.

The -q flag in wget stands for "quiet." It suppresses progress bars and download status messages, which is usually desirable for cron jobs. However, it does not suppress error messages generated by wget itself or errors returned by the remote PHP script if they are sent via HTTP headers or specific exit codes. If the PHP script fails to connect or times out, wget will return a non-zero exit code. By default, without -q, you would see "Connection refused" or similar messages in your output stream.

A critical limitation of using wget to call a CGI or server-side script is that you are relying on the HTTP response from the web server as the confirmation of success. If your PHP script sends an email and creates files but encounters a logic error, it might still return an HTTP 200 OK status code if there is no explicit error handling in the PHP code to alter the header. Consequently, wget will report success even though your side effects (email, file creation) failed.

To mitigate this, you should check the exit code of the command within your cron job. Cron jobs typically run as a specific user; ensure that user has read/write permissions on the log file and the target directories. If the PHP script relies on session data or cookies to maintain state between requests, wget might not handle these headers correctly without proper cookie jar management, leading to intermittent failures.

For more robust integration, especially if you are building a larger application ecosystem, consider using Laravel's HTTP client capabilities if you have control over the server-side code. Using curl directly with verbose logging options (-v) can also provide better visibility into handshake issues than wget in headless cron environments. Always ensure that your log file path is absolute and writable by the user executing the cron task to avoid permission errors that go unnoticed until manual inspection.

Finally, verify that the PHP script does not rely on input from stdin or environment variables set only in an interactive shell. Cron jobs run with a stripped-down environment; missing libraries or configuration files can cause silent failures where the script exits immediately after connecting.

Our network