R2D2
R2D2

Reputation: 2640

php script works fully when called manually, but won't run from cron job

I have a PHP script that essentially copies a zip file, unzips it, and then copies the resulting text files to a MySQL database.

This works perfectly when I call my php page manually, but this needs to be done every day, so I want to set this up as a cron job.

I have a cron job that works, and I have verified that by calling a very simple script, but it fails when trying to run my full page.

I have tracked this down to two areas of code. Firstly:

date_default_timezone_set('Europe/London');

I understand that I can set this up through an htaccess or php.ini file, so I am not concerned about this one.

However, the second area of code that is stopping the cron job from running is:

$localzipfile = "myfolder/myzipfile.zip";
$localzipfilefullpath = "/homepages/20/dXXXXXXXXX/htdocs/sub/folder/" . $localzipfile . "";
$localpath = pathinfo(realpath($localzipfile), PATHINFO_DIRNAME);
$zip = new ZipArchive;
$res = $zip->open($localzipfilefullpath);
if ($res === TRUE) {
$zip->extractTo($localpath);
$zip->close();
}

Again, this all works perfectly when I run the code manually, so I know everything is in place and works. All paths are correct, and the zip file unzips as expected.

It is only when I try to run this as a cron job that it fails and doesn't unzip the file.

I have seen several other SO questions about php files that run manually but don't run as cron jobs, but none that relate to the unzipping of a local zip file.

UPDATE:

I have managed to log the error output from the cronjob, and it is reporting this:

"Cannot instantiate non-existent class: ziparchive"

I don't understand this, though, as the code all runs without issue when I run it from the browser?

Upvotes: 0

Views: 1241

Answers (3)

R2D2
R2D2

Reputation: 2640

Finally managed to get to the bottom of this, after much testing and investigating!

Thanks to all who gave suggestions, but it seemed that when I was running the script through a browser, it was running at PHP version 5.4, but when the cron job was running, it was running at version 4.9!

Changing my cronjob calling script from:

0 * * * * /usr/bin/php

to

0 * * * * /usr/bin/php5

solved the zip problem, and the cronjob now unzips my file correctly!

Upvotes: 1

baao
baao

Reputation: 73301

The paths to your file need to be absolute, for example:

$localzipfile = "/home/myfolder/myzipfile.zip";
$localzipfilefullpath = "/var/www/html/homepages/20/dXXXXXXXXX/htdocs/sub/folder/" . $localzipfile;

Are you sure that the user running cron has permissions to access both directories? Cron might run under a different user which cannot access your /myfolder or /html dir, so make sure to give the users apropriate rights. You can see those two answers Answer1 | Answer2 I posted some time back on how to set up permissions.

Upvotes: 1

devnull
devnull

Reputation: 608

Are you using relative pathes for $localzipfile and $localpath?

If yes, try to use absolute ones or write a shell script that chdirs into the PHP script's directory before running PHP.

Upvotes: 1

Related Questions