Link to home
Start Free TrialLog in
Avatar of chivalry21
chivalry21Flag for United Kingdom of Great Britain and Northern Ireland

asked on

PHP, Cron, exec command not found

Hi All,

We are running a cron job that runs a PHP script.  The PHP script is used to create an array of filenames (that exist in the webserver folder) and then it runs an exec command to unzip the files.

All my paths are absolute and I can't see any problems or issues.  I've searched high and low for an answer and can't seem to find anything similar to what I'm trying to do.  The folders and files being called are CHMOD'd to 0777 so permissions *shouldn't* be an issue.  However the cron fails with "/gunzip: not found".

Could anyone point me in the right direction?

## Cron command
/usr/local/bin/php -q /usr/local/www/xxx/xxx/xxx/xxx/xxx/xxx.php
 
## PHP script
foreach($arrzipfiles as $zipfile){
exec('/usr/bin/gunzip '.$DIRECTORY_ROOT.$zipfile.'.csv.gz');
}

Open in new window

Avatar of MikeOM_DBA
MikeOM_DBA
Flag of United States of America image


Check if you have set the correct directory (location) to the $zipfile's in the PHP program

 
Avatar of chivalry21

ASKER


Path is absolute and is correct when browsing via shell.
Hi Mike,

Could it be a permissions problem somewhere with the cron user (apache?) not having enough permission to access the files?  They are set to 0777 but possibly that's not enough?

I'm not running safe mode for this particular domain.  When I echo the exec command I can execute it OK though shell as - su, the script also works when accessing it through a browser but it just doesn't want to work when I'm accessing it through Cron.

Any ideas?
is safe_mode, safe_mode_exec_dir or open_basedir set in your your php?
Hi hernst42,

- safe_mode is set globally but not locally
- safe_mode_exec_dir is not set
- open_basedir is set to local directory and /tmp:/var/tmp

Since the file I'm trying to unzip is within the local director I didn't think this would be a problem.  I'm not that well versed on Cron jobs so could safe_mode/open_basedir be the issue?

What is full crontab entry?
Does it contain one whitespace between time fields and eol after line?
What error was sent and logged by cron?
Hi gheist,

I'm starting to think this may be a permissions error.  I use Plesk and the cron entry was configured in the cron tab, output shown below.

The error I'm getting by email is also below.

I can see and understand that safe mode seems to be preventing rename(), however /gunzip not being found is a bit confusing.  The error message does not contain the full path to /gunzip that I'm specifying in the PHP code executed by my cron job.

I'm still stumped though.
4 13 * * * /usr/local/bin/php -q /usr/local/www/xxx/xxx/xxx/xxx/xxx/xxx.php
 
-----------
EMAIL ERROR
-----------
/gunzip: not found
PHP Warning:  rename(): SAFE MODE Restriction in effect.  The script whose uid is XXXX is not allowed to access /usr/local/www/xxx/xxx/xxx owned by uid 0 in /usr/local/www/xxx/xxx/xxx/xxx/xxx

Open in new window

safe_mode also prevents you to run commands which are not available in safe_mode_exec_dir dir.
Simple - fix your PHP code.
Do not touch others files, do not run untrusted programs you do not want to run.
OR
use your own PHP.INI, disable safe mode there and cross you fingers PHP 6.0 will never be released, since it will always run safe mode.
Hi All,

Thanks for the info.  From what you are suggesting the problem lies in the fact that safe_mode is running and covers the apache www account which is the one accessing files and commands under the Cron job?

However, safe mode has been turned off for the local level website, do I need to turn off safe mode globally?

gheist, I'm not sure I understand your comment.  How can I fix my PHP code if safe_mode is a php.ini configuration value?  Also, my understanding was that safe_mode was removed in PHP6, not that it would run all the time?
The php file runs correctly when accessed through a browser with no errors.
You can use any php.ini file from command line using -c option.

After all it turns out you complain your code runs without safe_mode and throws errors when running with safe_mode.
Still problem in your PHP code.
Hi gheist,

I'm not "complaining", I'm here for advice.  I haven't changed any safe_mode settings.  The code runs fine through a web browser but does not run correctly when run as a cron job.   You can find my code below and I would be grateful if you could point out where the problem lies?
<?PHP
$DOCUMENT_ROOT = '/usr/local/www/vhosts/xxx/httpdocs/';
$DIRECTORY_ROOT = $DOCUMENT_ROOT.'xxx/xxx/';
 
// DATAFIELD NAME
$datafeedname = 'awdatafeed_'.date("Y-m-d");
 
// UNZIP TO CSV AND RENAME ZIP FILE
exec('/usr/local/bin/unzip -ub '.$DIRECTORY_ROOT.$datafeedname.'.zip');
 
// RENAME CSV FILE
$arrFiles2 = scandir($DIRECTORY_ROOT);
foreach($arrFiles2 as $File){
	$ext = pathinfo($File, PATHINFO_EXTENSION);
	if($ext == 'csv'){
		rename($DIRECTORY_ROOT.$File,"$datafeedname.csv");
	}
}
 
// CHANGE PERMISSIONS ON FILES
chmod($DIRECTORY_ROOT.$datafeedname.'.csv', 0777);
 
?>

Open in new window

> safe mode has been turned off for the local level website

That explains your script runs from webserver and does not run from command line.

Since all your files are named xxx and xxx your code fragments make no sense e.g xxx/xxx is file in directory xxx/xxx/xxx and so on.
ASKER CERTIFIED SOLUTION
Avatar of hernst42
hernst42
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi hernst42,

I've tried your suggestion.  I copied the php.ini file into a separate folder and I'm now running the command using

/usr/local/bin/php -c /usr/local/www/vhosts/xxx/vhost/php.ini -f /usr/local/www/vhosts/xxx/httpdocs/xxx/xxx/unzip.php

 and I'm now getting this error below emailed to me.  I think it must be a permissions problem?
error:  cannot create datafeed-out.83762.csv

Open in new window

That is directory is not writable by user running script... Probably webserver uses /tmp
I've tried using gunzip and unzip, for some reason the gunzip works with .gz files using the cron job above but unzip does not work with .zip files with the error above.  I don't understand why it would work with one but not with the other?
I suggest you read manuals for utilities invoked and do not move too far from running php from cron.
PHP has its own zlib facilities and as such is able to process both .gz and .zip files without external utilities.