Cron job running?

Posted on 2012-08-30
Last Modified: 2012-10-31
I have a cron job set up via cPanel on a shared server set to run each night.
The cron job queries the database and writes data to a file. It also deletes all exisiting photos in a folder and copies images relating to the data written to the file. Then it ftps the file and images to another server.
This has been working fine and I've been receiving an email for the cron job each day.
I'm now no longer receiving the email and have no way of knowing if the cron job has run correctly. It appears to, in as much as the data file gets created and the images get copied. But I don't know if it successfully uploaded them all to the other server.
I have several echo statements within the code and , at the end of the code, I have text added to a report file. The cron job isn't echoing any of the echo statements or writing to the report file. When I run the code manually through a browser the same thing happens. And in fact the code never seems to stop, with the page constantly trying to load.

If I manually delete all the images in the folder first before running the script then I do get some echo statements, but again it appears to get stuck.

I've also tried running the code without the ftp upload and the same thing happens.

Is the code simply timing out?

Here's the code  (with sensitive stuff replaced with xxxxx)
#!/usr/local/bin/php -q



$branch_id = '20020';
// set up basic connection 
$ftp_server = "";
$ftp_user_name = "xxxxx";
$ftp_user_pass = "xxxxx";
$conn_id = ftp_connect($ftp_server); 

//set file name
$datenow = date(Ymd,time());
$filename = "12938".$datenow."01.blm";

//define directory
$dir = '/home/xxxxx/xxxxx/';

//delete images in folder
foreach(glob($dir.'*.jpg*') as $v){



//delete the file if it already exists
if (file_exists($dir.$filename)) { 

 //login with username and password 
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
// IMPORTANT!!! turn passive mode on
ftp_pasv ( $conn_id, true );

// check connection 
if ((!$conn_id) || (!$login_result)) { 
       echo "FTP connection has failed!<br />"; 
       echo "Attempted to connect to $ftp_server for user $ftp_user_name<br />"; 
   } else { 
       echo "Connected to $ftp_server, for user $ftp_user_name<br />"; 

  	or die ('I cannot connect to the database because: ' . mysql_error());

  	or die ('I cannot select the database because: ' . mysql_error());
  	$query = "SELECT id_num, department, city, prix, prop_type, nb_chambres, surface_h, surface_t, surface_a, dist_courses, title_en, head_desc_en, short_desc_en, desc_en, reduced_text FROM ventes WHERE etat=1 ORDER BY add_date desc";
  	$result = @mysql_query ($query);
  	while  (list($id_num,$department, $city, $prix,$prop_type, $nb_chambres,$surface_h,$surface_t,$surface_a,$dist_courses,$title_en,$head_desc_en,$short_desc_en,$desc_en,$reduced_text) = mysql_fetch_row($result)){
				$agent_ref = $branch_id.'_'. $id_num;
				$house_name_number = "0";
				$street_name = "0";
				$os_town_city = $city;
				$os_region = $department;
				$country_code = "fr";
				$feature1 = 'Living space: '.$surface_h.' m<sup>2</sup>;';
				$feature2 = 'Plot size: '.$surface_t.' m<sup>2</sup>;';
				$feature3 = 'Convertible space: '.$surface_a.' m<sup>2</sup>;';
				$feature4 = 'Distance to shops: '.$dist_courses.' km';
			 $summary = $title_en;
				$description = '<b>'.$head_desc_en.'</b><br /><br />'.$short_desc_en.'<br /><br />'.$desc_en;
				$create_date = "";
				$update_date = "";
				$status_id = 0;
				$bedrooms = $nb_chambres;
				$price = number_format($prix,0);
				$price_qualifier = 0;
				$prop_sub_id = $prop_type;
				$display_address = $n_city.', '.$n_department;
				$published_flag = 1;
				$trans_type_id = 1;
				$str ="$agent_ref^$house_name_number^$street_name^$os_town_city^$os_region^$country_code^$feature1^$feature2^$feature3^$feature4^$summary^$description^$create_date^$update_date^$branch_id^$status_id^$bedrooms^$price^$price_qualifier^$prop_sub_id^$display_address^$published_flag^$trans_type_id^";
			  fwrite($fp, $str);
				//deal with images here
				$imagecount = 0;
				$imageno = 1;
				While( $imagecount < 10 ){
				$propimagename = "$folder"."image_". "$imageno" . ".jpg";
				$newpropimagename = "$branch_id"."_" . "$id_num" . "_IMG_" . "0"."$imagecount".".jpg";
				if (file_exists("/home/xxxxx/public_html/image/".$propimagename)){
					 copy("/home/xxxxx/public_html/image/".$propimagename, $dir.$newpropimagename);

					 // upload the file 
	  			 $imagesourcepath = $dir.$newpropimagename;
	  		 	 $upload = ftp_put($conn_id, $newpropimagename, $imagesourcepath, FTP_IMAGE); 
				 		 //check upload status 
					 	 if (!$upload) { 
       		 		echo "FTP upload has failed for $propimagename as $newpropimagename!<br>"; 
   						$text1="****Not all photos uploaded successfully**** \n";
							} else{
							 echo "Upload $propimagename as $newpropimagename!<br>"; 
					fwrite($fp, $str);
			fwrite($fp, $str);
	fwrite($fp, "#END#");

//upload file
  $filesourcepath = $dir.$filename;
	$upload = ftp_put($conn_id, $filename, $filesourcepath, FTP_ASCII); 

//			 check upload status 
			if (!$upload) { 
       		echo "FTP upload has failed for $filename!"; 
					$text2="****Failed to upload $filename**** \n";
   				} else { 
       			echo "Uploaded $filename to $ftp_server as $filename"; 

//update report file
$reporttxt="Feed for xxxxx \n";
$reporttxt=$reporttxt."Number of properties in feed is ".$noProps." \n";
If (filemtime($filesourcepath)>$checkage){
  $reporttxt=$reporttxt."File $filename updated \n";
  $reporttxt=$reporttxt."****Failed to update $filename**** \n";
$myFile = "/home/xxxxx/bin/feedreport.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = $reporttxt." \n \n";
fwrite($fh, $stringData);


Open in new window

Question by:fionafenton
    LVL 34

    Assisted Solution

    by:Beverley Portlock
    Have you looked in the log files in /var/log/apache2? The default files are


    scan these for your script and see what shows up. You could use grep

    grep 'myscriptname.php' /var/log/apache2/error.log

    Failing that, modify the script to show error_reporting( E_ALL ) and then call it via a web browser and see what errors show up.
    LVL 107

    Expert Comment

    by:Ray Paseur
    You might try something like this...
    /* ... PROGRAM CODE HERE *.
    $msg = ob_get_contents();
    mail('', 'CRON', $msg);

    Open in new window

    It's possible that a change on the server causes the email to now be suppressed.
    LVL 1

    Author Comment

    I don't have access to
    with this hosting unfortunately.

    I have a couple of dozen crons running every night and it's only this one and one other that uses very similar script that I'm not getting emails from.

    Adding Ray_Paseur's code and running code manually has helped. I had forgotten to define $i and $noProps.
    The code is now running to the end, writing to the report file and emailing me the ob_get_contents. It's still not outputting anything to the browser, but I'm not worried about that as long as the code is doing what it should and updating the report file.

    I'll wait to see what happens when the cron job runs tonight.
    LVL 107

    Expert Comment

    by:Ray Paseur
    not outputting anything to the browser
    That would make sense with a CRON job, since it is not triggered by a browser.  It is an asynchronous script.  Another example of a script with no browser output is the PayPal Instant Payment Notification, which is triggered by a POST request.  It can use data bases and files, but there is no place to send browser output, so it has none.
    LVL 1

    Author Comment

    It's not outputting anything to the browser when I run it manually through my browser.

    The cron job ran fine for 2 nights, however last night it didn't. This time it definitely didn't complete. The .blm file it creates isn't large enough, so the script must have halted before completing. So I also didn't get the email with ob_get_contents(), so no clues as to what went wrong.

    Would it be better if I amended the code so that it first loops to write the .blm file and then loops again to ftp the files? Or maybe check the ftp connection is still open before each upload? I'm wondering if a loss of the ftp connection is the cause of the problem?
    LVL 107

    Accepted Solution

    It's not outputting anything to the browser when I run it manually
    Try adding these two lines to the top of the code.  Since there are echo statements in the script you should be seeing browser output when you run this script from the browser.

    ini_set('display_errors', TRUE);

    A hiccup on the FTP connection is certainly possible.  I haven't gone through all the code to see how it would handle the FTP part of things.  But I think you're on the right track to separate the creation of the .blm file from the FTP process.
    LVL 1

    Author Closing Comment

    As a follow up to this. It's taken me ages to track down what the problem was. I eventually discovered that the corns were automatically getting killed by the hosting server firewall because of excessive usage of memory while running the process.
    They have now exempted me from this limit and now the crons are running fine.
    (Have split points between those who offered help - thank you)

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now