Link to home
Start Free TrialLog in
Avatar of fionafenton
fionafentonFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Cron job running?

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

<?

$abserver="localhost";
$abadmin="xxxxx";
$abpass="xxxxx";
$abbdd="xxxxx";

$branch_id = '20020';
date_default_timezone_set('Europe/London');
// set up basic connection 
$ftp_server = "xxxxx.xxxxx.com";
$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){

    unlink($v);

}

//delete the file if it already exists
if (file_exists($dir.$filename)) { 
unlink($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 />"; 
       exit; 
   } else { 
       echo "Connected to $ftp_server, for user $ftp_user_name<br />"; 
   } 
 

$fp=fopen($dir.$filename,"a");
fwrite($fp, "#HEADER#\nVersion : 3i\nEOF : '^'\nEOR : '|'\n\r#DEFINITION#\nAGENT_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^MEDIA_IMAGE_00^MEDIA_IMAGE_01^MEDIA_IMAGE_02^MEDIA_IMAGE_03^MEDIA_IMAGE_04^MEDIA_IMAGE_05^MEDIA_IMAGE_06^MEDIA_IMAGE_07^MEDIA_IMAGE_08^MEDIA_IMAGE_09^|\n\r#DATA#\n");
  
$mysql=mysql_connect($abserver,$abadmin,$abpass) 
  	or die ('I cannot connect to the database because: ' . mysql_error());

  $select=mysql_select_db($abbdd,$mysql)
  	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)){
		   						
	  	$i++;
	 $noProps++;
	 
							
				$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;
				$folder=sprintf("%05d",$id_num).'/';
				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;
					 $text1="";
					 If(file_exists($imagesourcepath)){
	  		 	 $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>"; 
							}
						}
				}
	  			
					$str="$newpropimagename^";
			
					fwrite($fp, $str);
					
  		 
					
				 $imagecount++;
				 $imageno++;
				
	  	  }
				
	  	$str="|\n\r";		
			
			fwrite($fp, $str);
	}
	
	fwrite($fp, "#END#");
	
 	fclose($fp);
  	

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


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

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

  	
  	?>
  	
  	

Open in new window

SOLUTION
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You might try something like this...
<?php
error_reporting(E_ALL);
ob_start();

/* ... PROGRAM CODE HERE *.

$msg = ob_get_contents();
mail('You@Your.org', 'CRON', $msg);

Open in new window

It's possible that a change on the server causes the email to now be suppressed.
Avatar of fionafenton

ASKER

I don't have access to
/var/log/apache2/access.log
/var/log/apache2/error.log
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.
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.
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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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)