Execute PHP script in cron job

Execute PHP script in cron job

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

Stefan Bogdanescu

Founder & Senior Architect

Execute PHP script in cron job

Running a PHP script via crontab under the apache user is a common requirement for background tasks, but it often fails silently if the execution environment isn't identical to the web server environment. In your specific scenario on CentOS 6, the command php /opt/test.php executed by the apache user returns an error, even though the same script runs perfectly when triggered as a direct HTTP request. The issue rarely lies with the PHP binary itself; replacing php with /usr/local/php/bin/php usually doesn't resolve it because the underlying problem is environmental, not binary-path-related.

When Apache processes a request, it loads a specific configuration stack that initializes the session handler, mailer settings, and most importantly for this case, the open_basedir restrictions if they are enabled. Cron jobs, however, run in a minimal shell environment. The user context is different, and while you correctly identified that the script owner must be apache (verified by editing crontab with crontab -u apache -e), the working directory and loaded libraries often differ.

A frequent pitfall in this setup involves the open_basedir directive found in your Apache configuration files (httpd.conf or apache2.conf). If your PHP installation restricts file access to specific directories, the cron job might be running from a different root (typically /) and attempting to write to /opt/test/test.txt. While the web request starts in /var/www/html, the cron job starts in the user's home directory or /tmp. If open_basedir is set to include only document roots like /var/www, the cron job will fail immediately with a permission error, even if the binary path is correct.

To diagnose this, check the Apache configuration for the open_basedir directive:

grep -r "open_basedir" /etc/php.ini /etc/httpd/conf.d/

If open_basedir is active and restrictive, you must either add /opt to the allowed list or ensure the cron job runs in a directory that is already permitted. Another critical factor is the PATH variable. Cron jobs often have a stripped-down PATH that might not include the location of your custom PHP binary (/usr/local/php/bin/). While you attempted to use the full path, there are still environment variables like LD_LIBRARY_PATH or missing system libraries that Apache loads but which the cron daemon does not automatically inject.

You can inspect the actual error logs to confirm the failure mode. Check /var/log/httpd/error_log (or /var/log/messages) for entries matching the scheduled time. You will likely see messages regarding "open_basedir restriction" or "failed to initialize mailer". If the script simply does nothing, it might be due to umask settings preventing file creation, though your script uses exec() which spawns a new shell process and should inherit permissions unless restricted by open_basedir.

The most robust solution involves ensuring the cron entry mimics the web server's environment as closely as possible. Instead of relying on defaults, explicitly set the working directory and configure the necessary environment variables within the crontab file itself:

SHELL=/bin/bash
PATH=/usr/local/php/bin:/usr/bin:/bin
cd /opt
24 17 * * * php /test.php >> /opt/test/cron.log 2>&1

Note that changing the working directory with cd inside a cron job is effective only if the command following it respects that change. However, the most reliable method for complex PHP applications is to use the full path to PHP and ensure the open_basedir restriction explicitly includes the target output directory. If you are using Laravel or a similar framework, these restrictions can break routing and database connections if the config root isn't accessible from the cron context.

Finally, consider whether running as the apache user is strictly necessary for this specific task. If the script only needs to write to /opt/test, creating a dedicated service account with read/write access to that directory might be cleaner than forcing Apache's user into a restricted file system role. However, if security policies require isolation, sticking with the apache user requires meticulous attention to the open_basedir and library paths configuration.

Our network