Index: /gsdl/trunk/perllib/downloaders/WgetDownload.pm
===================================================================
--- /gsdl/trunk/perllib/downloaders/WgetDownload.pm	(revision 17546)
+++ /gsdl/trunk/perllib/downloaders/WgetDownload.pm	(revision 17547)
@@ -100,5 +100,5 @@
 	close($chld_in);
 	
-	#print STDOUT "Received termination signal: $termination_signal\n";
+	print STDOUT "Received termination signal: $termination_signal\n";
 
 	# Send TERM signal to child process to terminate it. Sending the INT signal doesn't work
@@ -240,5 +240,5 @@
 	} 
 	else { # wget finished, terminate naturally
-	    #print STDERR "\nPerl: open2 command, input stream closed. Wget terminated naturally.\n";
+	    #print STDOUT "\nPerl: open2 command, input stream closed. Wget terminated naturally.\n";
 	    close($chld_in);
 	    close($chld_out);
@@ -269,5 +269,5 @@
 		print $client "Talked to ServerSocket (port $port). Connection accepted\n";
 		
-		# Read from the client (getting rid of trailing newline)
+		# Read from the client (getting rid of the trailing newline)
 		# Has the client sent the <<STOP>> signal?
 		my $signal = <$client>;
@@ -275,18 +275,31 @@
 		if($signal eq "<<STOP>>") {
 		    print $client "Perl received STOP signal (on port $port): stopping wget\n";
+		    $loop = 0;
 		    
-		    $loop = 0;
-		    close($chld_in);
-		    close($chld_out);
-		    kill("TERM", $childpid);
-		    
-		    $childpid = undef;
-
-		    # Stop monitoring the read_handle
-		    # close the serverSocket (the Java end will close the client socket that Java opened)
-		    $read_set->remove($rh); #$read_set->remove($serverSocket);
-		    close($rh); 	    #close($serverSocket);
-		    #print $client "Perl is about to exit\n";
-		    last;
+		    # Sometimes the wget process takes some time to start up. If the STOP signal
+		    # was sent, don't try to terminate the process until we know it is running. 
+		    # Otherwise wget may start running after we tried to kill it. Wait 5 seconds
+		    # for it to start up, checking for whether it is running in order to kill it.
+		    for(my $seconds = 1; $seconds <= 5; $seconds++) {
+			if(kill(0, $childpid)) { 
+			    # If kill(0, $childpid) returns true, then the process is running
+			    # and we need to kill it.
+			    close($chld_in);
+			    close($chld_out);
+			    kill("TERM", $childpid);
+			    
+			    $childpid = undef;
+			    
+			    # Stop monitoring the read_handle and close the serverSocket 
+			    # (the Java end will close the client socket that Java opened)
+			    $read_set->remove($rh);     #$read_set->remove($serverSocket);
+			    close($rh); 	        #close($serverSocket);
+			    print $client "Perl terminated wget and is about to exit\n";
+			}
+			else { # the process may just be starting up, wait
+			    sleep(1);
+			}
+		    }
+		    last; # we're done here
 		}
 	    }
@@ -410,18 +423,31 @@
 		if($signal eq "<<STOP>>") {
 		    print $client "Perl received STOP signal (on port $port): stopping wget\n";
+		    $loop = 0;
 		    
-		    $loop = 0;
-		    close($chld_in);
-		    close($chld_out);
-		    kill("TERM", $childpid);
-		    
-		    $childpid = undef;
-
-		    # Stop monitoring the read_handle
-		    # close the serverSocket (the Java end will close the client socket that Java opened)
-		    $read_set->remove($rh); #$read_set->remove($serverSocket);
-		    close($rh); 	    #close($serverSocket);
-		    #print $client "Perl is about to exit\n";
-		    last;
+		    # Sometimes the wget process takes some time to start up. If the STOP signal
+		    # was sent, don't try to terminate the process until we know it is running. 
+		    # Otherwise wget may start running after we tried to kill it. Wait 5 seconds
+		    # for it to start up, checking for whether it is running in order to kill it.
+		    for(my $seconds = 1; $seconds <= 5; $seconds++) {
+			if(kill(0, $childpid)) { 
+			    # If kill(0, $childpid) returns true, then the process is running
+			    # and we need to kill it.
+			    close($chld_in);
+			    close($chld_out);
+			    kill("TERM", $childpid);
+			    
+			    $childpid = undef;
+			    
+			    # Stop monitoring the read_handle and close the serverSocket 
+			    # (the Java end will close the client socket that Java opened)
+			    $read_set->remove($rh);     #$read_set->remove($serverSocket);
+			    close($rh); 	        #close($serverSocket);
+			    print $client "Perl terminated wget and is about to exit\n";
+			}
+			else { # the process may just be starting up, wait
+			    sleep(1);
+			}
+		    }
+		    last; # we're done here
 		}
 	    }
