--- loncom/loncron	2001/04/21 15:22:37	1.20
+++ loncom/loncron	2001/08/27 13:54:50	1.22
@@ -11,7 +11,8 @@
 # 2/8 Gerd Kortemeyer
 # 12/6/2000,12/8 Scott Harrison
 # 12/23 Gerd Kortemeyer
-# 1/10/2001, 2/12/, 2/26, 3/15, 04/11, 04/21 Scott Harrison
+# YEAR=2001
+# 1/10/2001, 2/12/, 2/26, 3/15, 04/11, 04/21,8/27 Scott Harrison
 
 use IO::File;
 use IO::Socket;
@@ -283,9 +284,8 @@ print $fh "</pre>";
 
 
 # ---------------------------------------------------------------------- lonsql
-#
-# Do not run for now
-#
+
+my $restartflag=1;
 if ($perlvar{'lonRole'} eq "library") {
 
     print $fh '<hr><a name="lonsql"><h2>lonsql</h2><h3>Log</h3><pre>';
@@ -310,11 +310,31 @@ if ($perlvar{'lonRole'} eq "library") {
 	chomp($lonsqlpid);
 	if (kill 0 => $lonsqlpid) {
 	    print $fh "<h3>lonsql at pid $lonsqlpid responding</h3>";
+	    $restartflag=0;
 	} else {
 	    $errors++; $errors++;
 	    print $fh "<h3>lonsql at pid $lonsqlpid not responding</h3>";
+	    # Intelligently handle this.
+	    # Possibility #1: there is no process
+	    # Solution: remove .pid file and restart
+	    if (getpgrp($lonsqlpid)==-1) {
+		unlink($lonsqlfile);
+		$restartflag=1;
+	    }
+	    else {
+		# Possibility #2: there is a live process that is not
+		# responding for an unknown reason
+		# Solution: kill parent and children processes, remove .pid
+		# and restart
+		`killall -9 lonsql`;
+		unlink($lonsqlfile);
+		$restartflag=1;
+	    }
 	}
-    } else {
+	print $fh 
+	    "<h3>Deciding to clean up stale .pid file and restart lonsql</h3>";
+    }
+    if ($restartflag==1) {
 	$errors++;
 	print $fh "<h3>lonsql not running, trying to start</h3>";
 	system(
@@ -387,7 +407,7 @@ print $fh "</pre>";
 
 my $londfile="$perlvar{'lonDaemons'}/logs/lond.pid";
 
-my $restartflag=1;
+$restartflag=1;
 if (-e $londfile) {    
    my $lfh=IO::File->new("$londfile");
    my $londpid=<$lfh>;
@@ -680,7 +700,8 @@ if ($totalcount>200) {
    $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
    $subj="LON: $perlvar{'lonHostID'} E:$errors W:$warnings N:$notices"; 
    system(
- "metasend -b -t $emailto -s '$subj' -f $statusdir/index.html -m text/html");
+ "metasend -b -t $emailto -s '$subj' -f $statusdir/index.html -m text/html")
+    unless $qflag;
 }
 1;