Question

I have the following php and .sh file which access an FTP server, downloads jpg files, puts them to another server then deletes them from the temporary folder. This works great when I run the script through my browser but I'm trying to add it as a cronjob.

I've set my crontab to:

50 16 * * 1-5 /location/of/script/transfer.php

And I can see from the cron log that it has requested the file but that's as far as things go, no images have been transfered, am I doing something blatantly obvious wrong?

transfer.php

<?php
echo shell_exec('sh /location/of/script/ftp_jpg_fetch.sh');
$files = glob('/location/of/script/*.jpg');
foreach($files as $file){
  if(is_file($file))
    unlink($file);
}
$files2 = glob('/location/of/script/*.JPG');
foreach($files2 as $file2){
  if(is_file($file2))
    unlink($file2);
}
mail('myemail@mydomain.com','Images transferred','All images from the FTP have been transferred to the other server.');
?>

ftp_jpg_fetch.sh

#!/bin/sh
USER='username1'
PASSWD='password1'
ftp -n -i ftp.host1.com <<SCRIPT
user $USER $PASSWD
binary
cd vendora
mget *.jpg
mget *.JPG
cd /
cd vendorb
mget *.jpg
mget *.JPG
quit
SCRIPT

USER2='username2'
PASSWD2='password2'
ftp -n -i ftp.host2.com <<SCRIPT2
user $USER2 $PASSWD2
binary
cd htdocs/test
bin
mput *.jpg
mput *.JPG
quit
SCRIPT2
Was it helpful?

Solution

When run from CRON, you do not invoke the PHP script directly, but PHP's executable with the script passed to it. E.g:

50 16 * * 1-5 /usr/bin/php /location/of/script/transfer.php
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top