Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

Why does FTP and CRON job not working?

Hello , when i call the PHP script from browser it's working fine , but when i run it as a cron script its not working . cron script contains FTP commands as ftp connect the script die when reach FTP connect command . anybody can help

user-image
Question added by mohammed al shareif , Team Leader , Beecell
Date Posted: 2014/05/08
Mohd. Mahmood Hasan Khan
by Mohd. Mahmood Hasan Khan , Technology - Consultant , Deloitte

Hi,

Can you share your sample script that you have written and contains ftp commands in it while executing via cron job?

That will help us better understand the issue.

 

Thanks and regards,

Mahmood.

Technology - Consultant.

Manish Patel
by Manish Patel , Senior Software Architect , Impelsys Pvt Ltd

Best way to execute cron script is to create .sh file, set it with crontab -e. 

 

Do not forget to..

 

- Restart cron service

- Make sure you have +x permission set for .sh file

 

Muhammad Majid Saleem
by Muhammad Majid Saleem , Senior PHP Developer / Project Manager , SwaamTech

If your script is running perfectly fine from web browser and not working using cron job, that means your cronjob is definitely not correctly setup.

 

You should confirm that you have setup100% correct cron job. Please create a script to send an email to yourself and call that script using a cronjob. If you get an email, that means you have correctly setup cronjob. If not, that means cronjob didn't setup correctly. If you get it setup correctly, then repeat steps for your actual script.

Hello Mohammed,

 

If you didn't  get the the answer yet try this. It will run your PHP Script from browser

* * * * * wget -O- http://www.sitename.ccom/filename.php >> /var/log/invisibility.logJust put complete path of your Script and set appropriate timer. When Script will run it will save the logs of your webpage into invisiblity.log file here /var/log/invisibility.logLet me know does it work for youThanks

mohammed al shareif
by mohammed al shareif , Team Leader , Beecell

yes sure this is my script

///-------------

$ftp_server = "hidden";

$ftp_user = "hidden";

$ftp_pass = "hidden";

//

$date=time()-24*60*60;

$date= date('m-d-Y',$date);

$server_directory='/'.$date.'/Regular';

 

mkdir($date,0777);

 

mkdir($date.'/xmlFiles',0777);

$local_file=$date.'/data.tar.gz';

// set up a connection or die

/**/

$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); // connect to another server 

////hear the script is die when its called throw cron job

 

// try to login

if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {

  

if (ftp_chdir($conn_id, $server_directory)) {

 

$contents = ftp_nlist($conn_id, ".");///lisitng all content

 

} else { 

    echo "Couldn't change directory\\n";

}

$server_file=$contents[0];

 

if (ftp_get($conn_id,   $local_file,$server_file,FTP_BINARY)) {

   echo "Successfully written to $local_file\\n";

} else {

   echo "There was a problem\\n";

}

}

exec('tar -xvf '.$local_file .' -C  '.$date.'/xmlFiles');////unix command for extract the files 

 

 

ftp_close($conn_id);  

 

Asif Ali M
by Asif Ali M , Project Lead , LiquidHub

As Majid has already pointed out that the cron might not be set with proper syntax.

 

The other reason might  be user account permission.

Please be aware when you run your script from URL, its the apache/nobody (depends on OS/Hosting company) user who executes the script.

May be your account is not having the same execute permissions as Apache.Simply run the script from terminal and see what the output is.

 

Atif Majid
by Atif Majid , Senior PHP Programer/Team Lead , RedApple Apartments AB

1) Can you connect to the target FTP server from your local machine? 2) Are the ports between your server and the target machine open for FTP communication.3) Try to run the script in browser first with error_reporting and error display. Or use try/catch to find exceptions. Once you know the reason(s), you will be in better position to solve those.

More Questions Like This