LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-29-2008, 07:17 AM   #1
Black187
LQ Newbie
 
Registered: Nov 2005
Posts: 26

Rep: Reputation: 0
How to rerun php cli script after crash


Hello,

How can I rerun php cli script when it crashes (my php script runs continuous, but sometime connection to SQL crashes and the whole script crashes)?
 
Old 01-05-2009, 05:00 AM   #2
rylan76
Senior Member
 
Registered: Apr 2004
Location: Potchefstroom, South Africa
Distribution: Fedora 17 - 3.3.4-5.fc17.x86_64
Posts: 1,500

Rep: Reputation: 91
Hi

Here's how I do pretty much what you describe you want done:

Code:
<?php
//Cronned mailshot runner

session_start();

include 'localize.php';
include 'common.php';

include 'cms/constants.php';
include 'cms/general_functions.php';
include 'timefix.php';
include 'sanitize.inc.php';
include 'frontend_constants.php';
include 'frontend_functions.php';
include 'cms/fwf_constants.php';

require_once('cms/class.phpmailer.php');

$link = connect_to_db("rw");

//Check if there is a running mailshot

$check_query = "select * from mailshots where mailshot_status = 1";

$check_result = mysql_query($check_query) or die("Query failed in checking if any running mailshots exist:" . mysql_error());

$num_check_results = 0;
$num_check_results = mysql_num_rows($check_result);

if ($num_check_results == 0)
  {
    //I. e. there is no running mailshot

    //Check if there are any pending mailshots

    $check_query = "select * from mailshots where mailshot_status = 0";

    $check_result = mysql_query($check_query) or die("Query failed in checking if there are any pending mailshots:" . mysql_error());

    $num_check_results = 0;
    $num_check_results = mysql_num_rows($check_result);

    if ($num_check_results != 0)
      {
	//There are pending mailshots

	$check_row = mysql_fetch_array($check_result,MYSQL_ASSOC) or die("Failed to retrieve row with pending mailshot information:" . mysql_error());

	//Mark the mailshot as running

	$post = "update mailshots set mailshot_status = 1, mailshot_transmission_began_or_restarted_datetime=";
	if ($_SESSION['time_fix'] == TIME_NO_FIX)
	  {
	    $post .= "now()";
	  }

	else if ($_SESSION['time_fix'] == TIME_DE)
	  {
	    $post .= "timestamp(date(now()),addtime(time(now()),\"01:00:00\"))";
	  }

	else if ($_SESSION['time_fix'] == TIME_DE_2)
	  {
	    $post .= "timestamp(date(now()),addtime(time(now()),\"02:00:00\"))";
	  }

	$post .= " where mailshot_id=" . $check_row[mailshot_id];

	$post_result = mysql_query($post) or die("Query failed in marking a mailshot as running:" . mysql_error());
	//Loop and send

	$mail = new PHPMailer();

	$mail->From = "";

	$mail->FromName = "APROS.com";

	$mail->Subject = "APROS Mailshot";

	$mail->Body = $check_row[mailshot_message_text];

	switch ($check_row[mailshot_group_id])
	  {
	  case 0: //Everybody is being emailed
	    //Mail social investors

	    $query = "select id,email from socialinvestor where mailshot_status = 0 order by investor_name asc";
	    $result = mysql_query($query) or die("Query failed in retrieving a list of social investors to email:" . mysql_error());
	
	    $num_results = 0;
	    $num_results = mysql_num_rows($result);

	    while ($num_results > 0)
	      {
		$info_row = mysql_fetch_array($result,MYSQL_ASSOC) or die("Failed to retrieve row with socialinvestor information for emailing:" . mysql_error());

		//Send the email

		$mail->ClearAddresses();

		$mail->AddAddress($info_row[email]);

		if (!$mail->Send())
		  {
		    echo "Message mailing via cron to APROS social investor user failed! Please contact Polar Software Support.";
		  }

		//Wait a second to give queued mails time to dequeue

		sleep(1);

		//Mark this user as having been sent an email

		$post = "update socialinvestor set mailshot_status = 1 where id=" . $info_row[id];

		$post_result = mysql_query($post) or die("Query failed in marking a social investor as having been sent an email:" . mysql_error());

		//Update the mailshot last transmission time

		$post = "update mailshots set mailshot_last_transmission_time=" . time() . " where mailshot_id=" . $check_row[mailshot_id];

		$post_result = mysql_query($post) or die("Query failed in updating last mailshot transmission time for social investors:" . mysql_error());

		$num_results--;
	      }

	    //Mail verification agents

	    $query = "select id,email from verificationagent where mailshot_status = 0 order by namesurname asc";
	    $result = mysql_query($query) or die("Query failed in retrieving a list of verification agents to email:" . mysql_error());
	
	    $num_results = 0;
	    $num_results = mysql_num_rows($result);

	    while ($num_results > 0)
	      {
		$info_row = mysql_fetch_array($result,MYSQL_ASSOC) or die("Failed to retrieve row with verification agent information for emailing:" . mysql_error());

		//Send the email

		$mail->ClearAddresses();

		$mail->AddAddress($info_row[email]);

		if (!$mail->Send())
		  {
		    echo "Message mailing via cron to APROS verification agent user failed! Please contact Polar Software Support.";
		  }

		//Wait a second to give queued mails time to dequeue

		sleep(1);

		//Mark this user as having been sent an email

		$post = "update verificationagent set mailshot_status = 1 where id=" . $info_row[id];

		$post_result = mysql_query($post) or die("Query failed in marking a verification agent as having been sent an email:" . mysql_error());

		//Update the mailshot last transmission time

		$post = "update mailshots set mailshot_last_transmission_time=" . time() . " where mailshot_id=" . $check_row[mailshot_id];

		$post_result = mysql_query($post) or die("Query failed in updating last mailshot transmission time for verification agents:" . mysql_error());

		$num_results--;
	      }

	    //Mail steering committee members

	    $query = "select id,email from steering_committee where mailshot_status = 0 order by contact asc";

	    $result = mysql_query($query) or die("Query failed in retrieving a list of steering committee members to email:" . mysql_error());
	
	    $num_results = 0;
	    $num_results = mysql_num_rows($result);

	    while ($num_results > 0)
	      {
		$info_row = mysql_fetch_array($result,MYSQL_ASSOC) or die("Failed to retrieve row with steering committee member information for emailing:" . mysql_error());

		//Send the email

		$mail->ClearAddresses();

		$mail->AddAddress($info_row[email]);

		if (!$mail->Send())
		  {
		    echo "Message mailing via cron to APROS steering committee member failed! Please contact Polar Software Support.";
		  }

		//Wait a second to give queued mails time to dequeue

		sleep(1);

		//Mark this user as having been sent an email

		$post = "update steering_committee set mailshot_status = 1 where id=" . $info_row[id];

		$post_result = mysql_query($post) or die("Query failed in marking a verification agent as having been sent an email:" . mysql_error());

		//Update the mailshot last transmission time

		$post = "update mailshots set mailshot_last_transmission_time=" . time() . " where mailshot_id=" . $check_row[mailshot_id];

		$post_result = mysql_query($post) or die("Query failed in updating last mailshot transmission time for steering committee members:" . mysql_error());

		$num_results--;
	      }

	    //Mail sub administrators

	    $query = "select id,email from sub_admin where mailshot_status = 0 order by contact asc";

	    $result = mysql_query($query) or die("Query failed in retrieving a list of sub administrator members to email:" . mysql_error());
	
	    $num_results = 0;
	    $num_results = mysql_num_rows($result);

	    while ($num_results > 0)
	      {
		$info_row = mysql_fetch_array($result,MYSQL_ASSOC) or die("Failed to retrieve row with sub administrator information for emailing:" . mysql_error());

		//Send the email

		$mail->ClearAddresses();

		$mail->AddAddress($info_row[email]);

		if (!$mail->Send())
		  {
		    echo "Message mailing via cron to APROS sub administrator member failed! Please contact Polar Software Support.";
		  }

		//Wait a second to give queued mails time to dequeue

		sleep(1);

		//Mark this user as having been sent an email

		$post = "update sub_admin set mailshot_status = 1 where id=" . $info_row[id];

		$post_result = mysql_query($post) or die("Query failed in marking a sub committee member as having been sent an email:" . mysql_error());

		//Update the mailshot last transmission time

		$post = "update mailshots set mailshot_last_transmission_time=" . time() . " where mailshot_id=" . $check_row[mailshot_id];

		$post_result = mysql_query($post) or die("Query failed in updating last mailshot transmission time for sub committee members:" . mysql_error());

		$num_results--;
	      }

	    break;

	  case 2: //Social investors are being mailed
	    $query = "select id,email from socialinvestor where mailshot_status = 0 order by investor_name asc";
	    $result = mysql_query($query) or die("Query failed in retrieving a list of social investors to email:" . mysql_error());
	
	    $num_results = 0;
	    $num_results = mysql_num_rows($result);

	    while ($num_results > 0)
	      {
		$info_row = mysql_fetch_array($result,MYSQL_ASSOC) or die("Failed to retrieve row with socialinvestor information for emailing:" . mysql_error());

		//Send the email

		$mail->ClearAddresses();

		$mail->AddAddress($info_row[email]);

		if (!$mail->Send())
		  {
		    echo "Message mailing via cron to APROS social investor user failed! Please contact Polar Software Support.";
		  }

		//Wait a second to give queued mails time to dequeue

		sleep(1);

		//Mark this user as having been sent an email

		$post = "update socialinvestor set mailshot_status = 1 where id=" . $info_row[id];

		$post_result = mysql_query($post) or die("Query failed in marking a social investor as having been sent an email:" . mysql_error());

		//Update the mailshot last transmission time

		$post = "update mailshots set mailshot_last_transmission_time=" . time() . " where mailshot_id=" . $check_row[mailshot_id];

		$post_result = mysql_query($post) or die("Query failed in updating last mailshot transmission time for social investors:" . mysql_error());

		$num_results--;
	      }
	    break;

	  case 3: //Verification agents are being mailed
	    $query = "select id,email from verificationagent where mailshot_status = 0 order by namesurname asc";
	    $result = mysql_query($query) or die("Query failed in retrieving a list of verification agents to email:" . mysql_error());
	
	    $num_results = 0;
	    $num_results = mysql_num_rows($result);

	    while ($num_results > 0)
	      {
		$info_row = mysql_fetch_array($result,MYSQL_ASSOC) or die("Failed to retrieve row with verification agent information for emailing:" . mysql_error());

		//Send the email

		$mail->ClearAddresses();

		$mail->AddAddress($info_row[email]);

		if (!$mail->Send())
		  {
		    echo "Message mailing via cron to APROS verification agent user failed! Please contact Polar Software Support.";
		  }

		//Wait a second to give queued mails time to dequeue

		sleep(1);

		//Mark this user as having been sent an email

		$post = "update verificationagent set mailshot_status = 1 where id=" . $info_row[id];

		$post_result = mysql_query($post) or die("Query failed in marking a verification agent as having been sent an email:" . mysql_error());

		//Update the mailshot last transmission time

		$post = "update mailshots set mailshot_last_transmission_time=" . time() . " where mailshot_id=" . $check_row[mailshot_id];

		$post_result = mysql_query($post) or die("Query failed in updating last mailshot transmission time for verification agents:" . mysql_error());

		$num_results--;
	      }

	    break;

	  case 4: //Steering committee members are being mailed
	    $query = "select id,email from steering_committee where mailshot_status = 0 order by contact asc";

	    $result = mysql_query($query) or die("Query failed in retrieving a list of steering committee members to email:" . mysql_error());
	
	    $num_results = 0;
	    $num_results = mysql_num_rows($result);

	    while ($num_results > 0)
	      {
		$info_row = mysql_fetch_array($result,MYSQL_ASSOC) or die("Failed to retrieve row with steering committee member information for emailing:" . mysql_error());

		//Send the email

		$mail->ClearAddresses();

		$mail->AddAddress($info_row[email]);

		if (!$mail->Send())
		  {
		    echo "Message mailing via cron to APROS steering committee member failed! Please contact Polar Software Support.";
		  }

		//Wait a second to give queued mails time to dequeue

		sleep(1);

		//Mark this user as having been sent an email

		$post = "update steering_committee set mailshot_status = 1 where id=" . $info_row[id];

		$post_result = mysql_query($post) or die("Query failed in marking a verification agent as having been sent an email:" . mysql_error());

		//Update the mailshot last transmission time

		$post = "update mailshots set mailshot_last_transmission_time=" . time() . " where mailshot_id=" . $check_row[mailshot_id];

		$post_result = mysql_query($post) or die("Query failed in updating last mailshot transmission time for steering committee members:" . mysql_error());

		$num_results--;
	      }

	    break;

	  case 5: //Sub administrators are being mailed
	    $query = "select id,email from sub_admin where mailshot_status = 0 order by contact asc";

	    $result = mysql_query($query) or die("Query failed in retrieving a list of sub administrator members to email:" . mysql_error());
	
	    $num_results = 0;
	    $num_results = mysql_num_rows($result);

	    while ($num_results > 0)
	      {
		$info_row = mysql_fetch_array($result,MYSQL_ASSOC) or die("Failed to retrieve row with sub administrator information for emailing:" . mysql_error());

		//Send the email

		$mail->ClearAddresses();

		$mail->AddAddress($info_row[email]);

		if (!$mail->Send())
		  {
		    echo "Message mailing via cron to APROS sub administrator member failed! Please contact Polar Software Support.";
		  }

		//Wait a second to give queued mails time to dequeue

		sleep(1);

		//Mark this user as having been sent an email

		$post = "update sub_admin set mailshot_status = 1 where id=" . $info_row[id];

		$post_result = mysql_query($post) or die("Query failed in marking a sub committee member as having been sent an email:" . mysql_error());

		//Update the mailshot last transmission time

		$post = "update mailshots set mailshot_last_transmission_time=" . time() . " where mailshot_id=" . $check_row[mailshot_id];

		$post_result = mysql_query($post) or die("Query failed in updating last mailshot transmission time for sub committee members:" . mysql_error());

		$num_results--;
	      }

	    break;
	  }

	//Mark the mailshot as done

	$post = "update mailshots set mailshot_last_transmission_time = 0, mailshot_status = 2, mailshot_transmission_finished_datetime=";

	if ($_SESSION['time_fix'] == TIME_NO_FIX)
	  {
	    $post .= "now()";
	  }

	else if ($_SESSION['time_fix'] == TIME_DE)
	  {
	    $post .= "timestamp(date(now()),addtime(time(now()),\"01:00:00\"))";
	  }

	else if ($_SESSION['time_fix'] == TIME_DE_2)
	  {
	    $post .= "timestamp(date(now()),addtime(time(now()),\"02:00:00\"))";
	  }

	$post .= " where mailshot_id=" . $check_row[mailshot_id];

	$post_result = mysql_query($post) or die("Query failed in marking the mailshot as completed:" . mysql_error());

	//Clear applicable sent flags

	switch ($check_row[mailshot_group_id])
	  {
	  case 0: //Everybody was mailed
	    $clear_post = "update socialinvestor set mailshot_status = 0";

	    $clear_result = mysql_query($clear_post) or die("Query failed in marking socialinvestors mailshot flags as sent:" . mysql_error());	    

	    $clear_post = "update verificationagent set mailshot_status = 0";

	    $clear_result = mysql_query($clear_post) or die("Query failed in marking verification agent mailshot flags as sent:" . mysql_error());

	    $clear_post = "update steering_committee set mailshot_status = 0";

	    $clear_result = mysql_query($clear_post) or die("Query failed in marking steering committee mailshot flags as sent:" . mysql_error());

	    $clear_post = "update sub_admin set mailshot_status = 0";

	    $clear_result = mysql_query($clear_post) or die("Query failed in marking sub administrator mailshot flags as sent:" . mysql_error());

	    break;

	  case 2: //Social investors were mailed
	    $clear_post = "update socialinvestor set mailshot_status = 0";

	    $clear_result = mysql_query($clear_post) or die("Query failed in marking socialinvestors mailshot flags as sent:" . mysql_error());
	    
	    break;

	  case 3: //Verification agents were mailed
	    $clear_post = "update verificationagent set mailshot_status = 0";

	    $clear_result = mysql_query($clear_post) or die("Query failed in marking verification agent mailshot flags as sent:" . mysql_error());
	    
	    break;

	  case 4: //Steering committee members were mailed
	    $clear_post = "update steering_committee set mailshot_status = 0";

	    $clear_result = mysql_query($clear_post) or die("Query failed in marking steering committee mailshot flags as sent:" . mysql_error());
	    
	    break;

	  case 5: //Sub administrators were mailed
	    $clear_post = "update sub_admin set mailshot_status = 0";

	    $clear_result = mysql_query($clear_post) or die("Query failed in marking sub administrator mailshot flags as sent:" . mysql_error());
	    
	    break;
	  }

	//Mail the admin that the mailshot is complete

	$admin_query = "select name, email from super_admin";

	$admin_result = mysql_query($admin_query) or die("Query fialed in retrieving super admin information:" . mysql_error());

	$admin_info_row = mysql_fetch_array($admin_result,MYSQL_ASSOC) or die("Failed to retrieve row with super admin information:" . mysql_error());

	$admin_mailer = new PHPMailer();

	$admin_mailer->From = "info@prosperity.com";

	$admin_mailer->FromName = "prosperity.com Auto-Emailer";

	$admin_mailer->AddAddress($admin_info_row[email]);

	$admin_mailer->Subject = "Prosperity Mailshot " . $check_row[mailshot_id] . " Complete";

	$admin_mailer->Body = "Dear " . $admin_info_row[name] . "\n\nThe APROS mailshot \"mailshot " . $check_row[mailshot_id] . "\" has been transmitted.\n\nAPROS: http://alternativeprosperity.com";

	if (!$admin_mailer->Send())
	  {
	    echo "Successful mail transmission email to super admin has failed. Please contact Polar Software Support.";
	  }
      }
  }

else

  {
    //I. e. there is a running mailshot

    $check_row = mysql_fetch_array($check_result,MYSQL_ASSOC) or die("Query failed in retrieving running mailshot information:" . mysql_error());

    //Check if the running mailshot ran within the last 10 minutes

    $current_unixtime = time();

    if ($check_row[mailshot_last_transmission_time] <= ($current_unixtime - 600)) //60 seconds to a minute
      {
	//I. e. the mailshot has apparently hung or was killed by processwatch, resume it by setting
	//the status back to 0, and clearing the last transmission time. Within a minute with the next
	//cron run the mailshot should resume...

	$reset_post = "update mailshots set mailshot_last_transmission_time = 0, mailshot_status = 0 where mailshot_id=" . $check_row[mailshot_id];

	$reset_result = mysql_query($reset_post) or die("Query failed in resetting a mailshot:" . mysql_error());
      }
  }

mysql_close($link);
?>
I run processwatch on my server, this sometimes kills the running PHP CGI mailer process. The mailer process attempts to start each minute, called via the Linux Cron program. What happens is, whenever any mailer process starts up, it checks in the database if it is running "alone". Because an emailing can take several hours, it is possible that when an instance of the mailer process starts, another one is already around, sending a mail. So each time it checks if a sibling is running. If one is, it simply quits. This happens again the next minute.

However, if a mail process starts (see code above) and it finds that there is no sibling already running (by checking the database - see above) it checks if maybe there is an INCOMPLETE mail sequence. (I. e. if it is "alone" it checks if its dead sibling was busy with something) - and it then simply picks up where the "processwatch killed" mailer left off.

This sounds more or less what you want? I. e.

1. Put your script up so it is run each minute or whatever, by cron.
2. When your script starts, check if there is another script already running (i. e. one that still has its connection)
3. If there is, stop and exit the process.
4. If it is NOT, reconnect to the database and resume.

I'm not sure what you are doing, you might need to setup some mechanism so the "rejuvenated" script can "know" where to continue from viz a viz the previous script that lost its connection. You might also need to design the script so that it exits completely cleanly if it loses its connection.

Then again, you might want to approach this in what can be considered the more correct way as well... i. e. find out why your script loses its connection so frequently, and fix THAT...!

Regards

Last edited by rylan76; 01-05-2009 at 05:04 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
php script will not run correctly as cron, fine from cli dtra Linux - Software 2 12-22-2005 07:21 AM
How to rerun network configuration script? pvv Debian 5 09-06-2005 02:12 PM
cron issue, php script runs fine in browser and cli dtra Linux - Software 1 07-15-2005 05:45 AM
Want to rerun the network probe from install script rickh Debian 2 05-19-2005 09:57 AM
firewall script run at boot -> no masq, rerun manually -> masq worx Griffon26 Linux - Networking 2 06-24-2002 03:17 AM


All times are GMT -5. The time now is 06:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration