2014-05-06 34 views
0

我有下面的php和.sh文件,它访问FTP服务器,下载jpg文件,将它们放到另一个服务器上,然后从临时文件夹中删除它们。当我通过浏览器运行脚本时,这很好用,但我试图将它作为cronjob添加。脚本不能通过Crontab工作

我已经把我的crontab来:

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

我可以从cron的日志,它已请求的文件,但是这就是尽可能的东西去看看,没有图像已被转移,我在干嘛有些明显的错误?

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('[email protected]','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 
+0

您可以检查http://stackoverflow.com/tags/crontab/info的“调试”部分中的典型错误。完整路径,环境... – fedorqui

+0

php本身不可执行; Web服务器将php-intrepreter应用于它。也许试试wget /web/path/to/your/script/transfer.php – fast

+0

@fast你也可以调用php可执行文件并将它传递给php脚本的路径,它将解释脚本。 –

回答

2

当从cron运行,你不直接调用PHP脚本,但PHP的可执行文件传递给脚本它。例如:

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