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!
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
Related
I have a php script that works perfectly fine when run by the web server but when I run the script from the command line (or as a cron job) then it fails to find the file. The code that errors out is:
include('fpdf181/fpdf.php');
I tried including DIR but that does not work either
include(__DIR__ .'fpdf181/fpdf.php');
Can someone tell me how to include files for a script that will run from the command line
can you see this (PHP Cron Job: Including file not working?)
as the link, you use "require" and not include
Code:
require('./fpdf181/fpdf.php')
happy codeing.
You can use the full path to the file (independent of current directory / env).
include('/path/to/lib/fpdf181/fpdf.php');
I was able to execute my script by doing a cwd on the directory and going to the folder where the script was placed
I'm on GoDaddy shared hosting. In my cron job PHP script, I use require() to include some other files. The cron job PHP script works fine when I use it manually (via entering its url to the address bar), but when the cron job is performed, the cron script works fine, except it can't find the required files.
I have the following cron command:
/usr/local/bin/php -q /home/username/public_html/domainname/cron/script.php >/dev/null 2>&1
I read here that one option is to use full paths in require(). Another option is to modify the cron command, which is what I want to learn. My question is how to modify the above cron command so that my require() in the cron script will work (without using full paths).
Thanks.
This is probably the easiest way to fix it.
chdir(dirname(__FILE__)); //its been a while but I think __FILE__ works better with bind mounts and symlink but don't quote me on that.
//chdir(__DIR__);
I can't quite remember, but I think I had to use this one time for a wordpress site, that I made a cron job for. If I remember correctly it had some relative directories, from 3rd party plugins etc...
Other wise you can use __DIR__ before your files.
I never use relative directories, I either use a constant
define('BASE_DIR', __DIR__); //or something else besides __DIR__
Or I just use __DIR__ and never have these kinds of issues. I like knowing exactly where my files are.
require __DIR__.'/somefile.php';
require BASE_DIR.'/somefile.php';
This is not really a PHP issue, its more an issue of how cron calls the PHP file, because the relative paths will be relative to the location of the crontab file.
Summery
That said about using __DIR__ I know from my own experience, you can't control how third parties include files, so chdir is probably the only workable solution in that case.
Hope it helps.
rename() runs fine from the command line, but when run from cron job, the rename() does not. Since the connect.php file works I assume the cron job is in the right directory, but can't figure out why rename() doesn't work. I tried absolute paths and they didn't work:
<?php
include 'connect.php';
$oldlocation='xxx/xxx/'.$oldfilename;
$newlocation='yyyy/xxx/'.$newfilename;
$move=rename("$oldlocation","$newlocation");
The cron job: * * * * * /usr/bin/php /usr/xxx/xxx/xxx/xxx.php -q -f
I have no root access to the server. Should this be run through a SHELL script?
The current path while a cron execution is the home directory of the user which is running the cron process. See also this post.
Just change the relative path to an absolute and the issue is fixed.
The solution, not directly a directory issue (well sort of):
$oldlocation='xxx/xxx/'.$oldfilename;
has to be changed to:
$oldlocation='/xxx/xxx/'.$oldfilename;
I guess I was missing the first /
I've a peculiar situation I cannot get to the bottom of it.
I've cron.php that runs every 15 minutes. It must execute following code:
exec("php gmail-smtp.php >> basic-email-template/debug-mailer.log &");
All the other parts of the cron work fine, like require "config.php"; which makes me think relative urls work fine inside cron. But relative url inside exec() could be a problem, unfortunately I don't see anything in the logs, and I don't see anything inside my own debug-mailer.log
Any thoughts?
I tried it both on my local dev mac, and remote ubuntu server. Result is same, ergo no result.
Cron is run like this:
*/5 * * * * php /Library/WebServer/Documents/favwords/lib/cron.php
Seems like current working directory is not valid. You can check it out by using getcwd() function inside your cron.php script. If it runs at all. After verifing it, use chdir() function to change current working directory to valid one - the one where gmail-smtp.php is. Or simply use full paths, not relative ones.
As a customer in Plesk, I am attempting to run:
php -q /httpdocs/_external/export/test.php
From this tutorial: http://daipratt.co.uk/crontab-plesk-php/
I'm receiving the error
"php: command not found"
Is there something I need to enable from the main user or a different command I would need to use to run the script?
(also tried /bin/php with no luck, there is no php file in that dir)
"which php"
-/usr/bin/php
(when I use this dir I also get "no such file or dir" I guess since when I use / it's pulling from the customers root not the server root)
This answer will help you. My understanding is that Cron runs everything relative to itself, so you should always use absolute paths when running something from Cron.
Good luck, and happy holidays!