PHP File Runs Fine Manually But behaves unexpected on Cron Job - php

I have a dreamhost web panel and I am setting a cronjob to run my php script, but it shows weird errors, it looks like that it's not executing the file , but it's reading the file. The error is shown below, it comes as email to me
?php: No such file or directory [somesessionvariable]=1573: command
not found line 3: syntax error near unexpected token
"include/mydb.php"' line 3:require_once("include/mydb.php");
This is the subject of the mail:
Cron /usr/local/bin/setlock -n
/tmp/cronlock.3782446772.128284 sh -c $'/path /to/file/cases_cron.php'
the permission of the file is 755 and i have copied the path from other perfectly running cronjobs, so no issue to the file path as it's getting the file contents. I am not getting where the problem is

I should try to use an absolute path to your include file.
I'm sure the problem is about the include path not being set the same in file call and cron call.
You can do something like this :
require_once dirname(__FILE__)."/include/mydb.php";
__FILE__ is the path of the file where this line is executed, dirname() return the absolute path where the file is located.
Of course, you can use, as you want require, require_once, include, include_one

Your paths are probably off. Try to either avoid relative paths (include/file.php instead of /var/www/project/include/file.php) or change your working directory with chdir().

You can also put an 'cd' command into your cronjob ie:
cd /path/to/phpfile && php -f file.php

Ok, Guys, Thanks for all of your support, I am finally able to sort out the matter.
I was missing the following line on the top of the file
#!/usr/local/bin/php
Adding this line at the top of the php file, sorted my problem. But i don't know that what this line means to the script. But i copied it from other working crons.

Related

Issue with calling php script with cron

I have set up a cron to call a php script to run some download tasks at a regular interval.
The website is hosted at Bluehost. I have followed the instructions on how to set up a cron and that basically works fine, but the behaviour is different from when calling the script manually which i suspect has to do with directory settings. When using the cron i get errors:
Warning: copy(wp-content/uploads/feeds/full/1.csv): failed to open stream: No such file or directory in /home1/user1/public_html/import/custom-downloader.php on line 86
my php code is copy( $row["externalURL"] , 'wp-content/uploads/feeds/full/'. $localfilename );
I have also tried below with same result.
copy( $row["externalURL"] , $_SERVER['DOCUMENT_ROOT'].'wp-content/uploads/feeds/full/'. $localfilename );
I think think that i need to construct the path to /home1/user1/public_html/import/wp-content/uploads/feeds/full/ but i don't see how to do that.
First thing's first, make sure that 1.csv exists and that it can be read (there's read permissions on the file)
Second thing, double check if 'wp-content/uploads/feeds/full/1.csv' really is what you want to copy. Your error message indicates that the php script calling the copy method is /home1/user1/public_html/import/custom-downloader.php. However, the path you specified is relative. So your PHP script (barring any changes to the PATH to look for other directories) will look for /home1/user1/public_html/import/wp-content/uploads/feeds/full/1.csv -- which may not be what you intended!
This can be solved by using an absolute path to your wp-content folder, or by moving this script to the parent directory of wp-content.

PHP script (include) not working from command line

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

php script require() statement fetches file when running from browser but not when running from Plesk CRON job

I have a task script that works perfectly fine if i run it by visiting the php file from my browsers. However, when i attempt to run it via Plesk Task Scheduler it fail with a fatal error unable to load the require() file.
The require statement is a simple relative path:
require('../../../app.config.php');
the error is:
PHP Fatal error: require(): Failed opening required '../../../app.config.php'
I think this may be related to the include_path but i don't know much about it so a bit lost on that one.
Any help would be great!
relative pathes resolved by current directory. Solutions: Get script directory and combine with the relative path, something like:
require(dirname(__FILE__).'/../../../app.config.php');
You need to use absolute path. A different relative path may be produced if a script is called directly or the script is included in another script.
For example:
a.php is on path A/B/C the ../../ will result in in A/
b.php is on path A/B/C/D, if it inlcudes the a.php then the ../../ on a.php will result in A/B/.

Getting exec() PHP to work like it does on the command line

The following command, which generates a .pdf file from a .tex file, works from the command line but not when I run it with PHP. The file has the appropriate permissions and I'm able to run other commands with exec() so not sure what is going on.
$file_path='uploads/some-path';
$full_path='uploads/some-path/file.pdf';
$cmd ="pdflatex -output-directory ".$file_path.' '.$full_path;
exec($cmd);
The flag -output-directory place the file in the file_path rather than the root directory.
Is pdflatex in the search path? Perhaps try specifying the full path to the executable and see if that makes a difference.

file path issue when running via command line versus durect URL

We have a few processes that we run via the command line (CRON)
/usr/bin/php /path/to/site/root/index.php --uri=cli/fifteen/clear_apc
With a recent change to the index file:
require_once(../opt/local/config/setting.php)
PHP returns:
No such file or directory
However, running it directly from the browser (same file, path), it runs correctly, including that file:
www.site.com/cli/fifteen/clear_apc
Im wondering if there is a work around for that path issue, without having to include the entire path from root (/path/to/site/opt/local/config/setting.php)
The working directory is different when running under cron and apache. Relative paths are relative to the current working directory, so that's why your code is not working as expected.
You can try require_once(dirname(__FILE__) . "../opt/local/config/setting.php"). This should generate an absolute path starting from your file's location.
Check out all the magic constants here: http://php.net/manual/en/language.constants.predefined.php

Categories