1
votes

I have a PHP script that works fine when run from the browser, but when run as a cron job (which was set up via cPanel), it doesn't. The server is running Linux.

I've narrowed it down to an include statement:

$include_path = dirname(__FILE__) . "/";
$wpconfig = $include_path . 'blog/wp-config.php';
include($wpconfig);

I've also tried using realpath(dirname(FILE)) instead of just dirname(FILE), and just trying to include "blog/wp-config.php", but it makes no difference.

The PHP script resides in this folder:

/home2/USERNAME/public_html/DOMAIN/tools/

The file I need to include is in this folder, which is one level below the one where the PHP script resides:

/home2/USERNAME/public_html/DOMAIN/tools/blog/wp-config.php

I thought using an absolute path should work in a cron job, but apparently not.

I'm clearly doing something wrong (or stupid), so could someone please put me out of my misery and point me in the right direction.

Thanks!

1
Your path - an absolute path - is correct. You need an absolute path because the working directory of the cron job is the file system root, not the folder where your php script is located. You made it right. The problem seems to be something different. - hek2mgl
A couple of things to check: 1. var_dump or echo the path, so you can see if it actually the path you expected. 2. use require instead of include, because it will throw an error if including fails. Maybe that error will give some insights. 3. it might be a rights issue. If the user running the script doesn't have read rights on the directory or file, then including will fail. - GolezTrol
The path appears to be correct, and when I changed include to require, I saw no error messages (which is not what I was expecting). - Mark

1 Answers

4
votes

I had several problems running cron scripts from Cpanel with absolute paths (when having includes) and ended up changing to executing them as URL's, now everything is working fine:

My command

wget -O /dev/null http://www.example.com/somescript > /dev/null 2>&1