0

I am trying to execute a PHP script using crontab but it doesn't seem to work. I am running AWS EC2 Linux and here is the PHP script:

  FOREACH (GLOB("*.jpg") AS $filename) {
     ECHO "$filename size " . FILESIZE($filename) . "\n";
     UNLINK($filename);
  }

  FOREACH (GLOB("*.jpeg") AS $filename) {
     ECHO "$filename size " . FILESIZE($filename) . "\n";
     UNLINK($filename);
  }

  FOREACH (GLOB("*.gif") AS $filename) {
     ECHO "$filename size " . FILESIZE($filename) . "\n";
     UNLINK($filename);
  }

  FOREACH (GLOB("*.png") AS $filename) {
     ECHO "$filename size " . FILESIZE($filename) . "\n";
     UNLINK($filename);
  }

When I execute this script manually from a browser, it works normally. But it doesn't work using crontab

Here is my cron command:

00 * * * * php /var/www/html/*****/*****/delete.php

And here is the log:

Nov 28 02:12:01 ip-##-##-##-## CROND[#####]: (root) CMD (/usr/bin/php /var/www/html/*****/*****/delete.php)

What am I possibly doing wrong?

1
  • Your calls to glob aren't specifyng a full directory path, so they're looking for files in the current directory and you might need to make sure the script runs with the correct current directory. e.g. 00 * * * * cd <some-directory> && php /var/www/html/*****/*****/delete.php Commented Nov 28, 2015 at 2:27

2 Answers 2

1

I just had a similar issue... Cron is setup correct (I think) but is not running

It is now working for me:

edit /etc/crontab directly and be sure to check your paths. (You can even cd into the correct directory like this, perhaps that will solve your issue too..)

00 * * * * cd /var/www/html/*****/*****/ && php ./delete.php

Also check if just php will do the trick. call:

which php

to see the full path of PHP and use that instead.

Sign up to request clarification or add additional context in comments.

Comments

0

If it is working fine in browser then you can set the cron job as below,

00 * * * * wget -q -O /dev/null http://example.com/delete.php

this will work same like you calling in browser just change the "http://example.com/delete.php" with the url which you call in browser.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.