Hus
Hus

Reputation: 21

Executing PHP scripts using crontab

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?

Upvotes: 0

Views: 906

Answers (2)

kishan
kishan

Reputation: 138

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.

Upvotes: 0

MrG
MrG

Reputation: 382

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.

Upvotes: 1

Related Questions