Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

IIS Restart

by trell (Novice)
on Jul 13, 2001 at 21:04 UTC ( #96480=sourcecode: print w/ replies, xml ) Need Help??
Category: Web Stuff
Author/Contact Info Joseph Oaks josephmoaks@netscape.net
Description: We had need of automatically restarting IIS when it got to a state where it would not respond to request. This code uses the following... Unix command from MKS Toolkit, (there are others out there) this is to kill the PID when the net stop wont work. URI.pm (from CPAN) IIS.pm (written from scratch, feel free to upgrade and let me know of improvements) ActiveState Perl 5.6.1 I have this broken into three script, one is the .pm, the second is the actual restart script, and last is a script that checks the socket for content, then calls the restart script if needed.
IIS.pm

# File: iis.pm version 1.03
# Description: Perl Module for the ck.pl script
# Written by: Joseph Oaks
# Date written: 14 May 2001
# Last revised: 09 Jul 2001

package IIS;
use IO::Socket;
use POSIX;
use IO::Select;
use LWP::UserAgent;
use HTTP::Request;

=head1 NAME

INET - perl module used for the starting and stopping of Microsoft IIS

=head1 SYNOPSIS

inet.pl

=head1 DESCRIPTION

In some installations of IIS and third party application, IIS will con
+tinue
to increase its memory use to the point where it will stop responding.
+ This
module came about as a way to start and stop the IIS and to verify tha
+t said
services are running.

=head1 VARIABLES

Be sure to change your environment setting to match your system.
Example...
$server = '127.0.0.1';
$url = '/your homepage';
$port = '80';
$timeout = '15';

=head1 AUTHOR

Joseph Oaks E<lt>F<joseph_oaks@non.hp.com>E<gt>

=cut

$server = '127.0.0.1';
$url = '/computing/printcentral/eprise';
$full_url = 'http://127.0.0.1/computing/printcentral/eprise';
$port = '80';
$timeout = '15';
$date = strftime('%m/%d/%Y %H:%M:%S', localtime);

sub make {
my $connect;

  print "In make() \n";
  my $ua = new LWP::UserAgent;
  $ua->timeout($timeout);
  
  print "UA created with timeout $timeout \n";

  $request = new HTTP::Request('GET', $full_url);
  print "Created request \n";
  $connect = $ua->simple_request($request); 
  print "Got response: ".$connect->content."\n";

 
  return $connect->content;
  }

sub stop {
  my $str1 = `net stop iisadmin /y`;
  return $str1;
}

sub start {
  $str2 = `net start w3svc /y`;
  sleep 10;
  return $str2; 
}

sub pid {
  $str3 = `ps -ef | grep -v grep | grep inetinfo`;
#  print $str3, "\n"; 
  @arr = split /\s+/, $str3;

   return $arr[2]; 
  }

============================================================
inet.pl

# File: inet.pl version 2.0
# Description: Restart Script for IIS
# Written by: Joseph Oaks & Praveen Sinha
# Date written: 20 Apr 2001
# Last revised: 09 Aug 2001

use POSIX;
use IO::Socket;
use IIS;

$| = 1;
open FILE, ">>c:\inet_log.log";
while (1) {
  my $time = strftime('%m/%d/%Y %H:%M:%S', localtime);
  my $uptime = `c:/uptime.exe`;

print FILE "$time\n";
print FILE "$uptime\n";

####################
# IIS Stop Section #
####################

my $line = `ps -ef | grep -v grep | grep inetinfo`;
@arr = split /\s+/, $line;
my $PID = $arr[2];
print FILE "INETINFO running on $PID ...\n";
  if ($arr[2] =~ /\d+/) {
  print FILE "-- Attempting to stop WWW Services...\n";
  my $str = `net stop iisadmin /y`;
  print FILE "$str\n";
    if ($str =~ /\s+successfully.\s+/i) {
    print FILE "-- INETINFO Server has shutdown Successfully.\n\n";
    } else {
      print FILE "INETINFO $PID did not stop on its own...\n\n";
      `kill $PID`;
      print FILE "INETINFO Server has been killed!\n\n"; 
   }
  }

 
#####################################
# Check if Dr. Watson Error Section #
#####################################

my $dr = undef;
my $drpid = `ps -ef | grep -v grep | grep drwtsn32`;
@arr2 = split /\s+/, $drpid;
$dr = $arr2[2];
  if ($dr eq '') {
  print FILE "-- No Dr. Watson errors...\n\n";
  } else {
  if ($arr2[2] =~ /\d+/) {
    print FILE "-- Dr. Watson error exists... \n\n";
    print FILE "   Killing $dr \n"; 
     `kill $dr`;
 }
}


#####################
# IIS Start Section #
#####################

my $NEWPID = undef;
  if ($NEWPID eq '') {
  print FILE "-- Atempting to start W3SVC Web Server!\n";
  my $str2 = `net start w3svc /y`;
  print FILE "$str2\n";
  if ($str2 =~ /\s+successfully.\s+/i) {
  print FILE "   WWW Services have been started.\n";
  }

  my $line1 = `ps -ef | grep -v grep | grep inetinfo`;
  @arr1 = split /\s+/, $line1;
  $NEWPID = $arr1[2];
  print FILE "-- INETINFO is now running with new PID = $NEWPID.\n\n";
  }
  
#####################
# Web Check Section #
#####################

$connect = &IIS::make;
   if ($connect =~ /\s+eprise\s+/is) {
     close FILE;
     exit(1);
 }
}

============================================================
ck.pl

# File: ck.pl version 1.01
# Discription: Restart script for IIS / Eprise Web server
# Written by: Joseph Oaks & Praveen Sinha
# Date written: 14 May 2001
# Last revised: 29 Jun 2001


use POSIX;
use IIS;
use IO::Socket;

$| = 1;
while (1) {

$chk_iis = &IIS::pid;

print "IIS is running with a pid of: ", $chk_iis, "\n\n";

  if ($chk_iis eq '') {
    `c:/inet.pl`;
    sleep 60;

  } else {

print "Checking if the Web Content...\n\n";
$connect = &IIS::make;

  if ($connect eq '') {
    `c:/inet.pl`;
    sleep 60;
  } else {
  sleep 300;  
}
 # exit(1);
  close FILE;
}
} 

Replies are listed 'Best First'.
Re: IIS Restart
by rrwo (Friar) on Jul 17, 2001 at 02:05 UTC

    The Windows NT Resource Kit has a handy utility called kill.exe which is better than net stop... if the server crashes, net stop won't work because the service won't respond. I use the following in a .cmd file:

    kill inetinfo.exe kill mtx.exe net start w3svc

    What I'd love to find is a Perl script that runs as a service using Win32::Daemon and can test the server, and if anything does not respond, kill it without shell commands (can't do that as a service!) and restart the server.

Re: IIS Restart
by Anonymous Monk on Sep 28, 2015 at 06:33 UTC
    Hi, It seems to be very useful. But i am not good in perl and wanted to know how can we arrange these script setup to work perfectly on the server. So that we won't get any outage. Thank for you help!
      it is not very useful. Maybe 14 years ago it was.

      Anyway if you are interested on the matter, please follow the Corion's advice about the overall design of a web infrastructure.

      Fore the mere restart of an IIS instance you just need one command.
      iisreset [computername] /RESTART Stop and then restart all Internet services. /START Start all Internet services. /STOP Stop all Internet services. /REBOOT Reboot the computer. /REBOOTONERROR Reboot the computer if an error occurs when st +arting, stopping, or restarting Internet services. /NOFORCE Do not forcefully terminate Internet services +if attempting to stop them gracefully fails. /TIMEOUT:val Specify the timeout value ( in seconds ) to wa +it for a successful stop of Internet services. On exp +iration of this timeout the computer can be rebooted i +f the /REBOOTONERROR parameter is specified. The default value is 20s for restart, 60s for +stop, and 0s for reboot. /STATUS Display the status of all Internet services. /ENABLE Enable restarting of Internet Services on the local system. /DISABLE Disable restarting of Internet Services on the local system.
      This command i think it ship with every installation of the headache-generator-webserver (Aka IIS) from times of Winnt and is still valid with IIS 7.5. The command can operate also on a remote machine.

      L*
      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      The traditional approach to zero-downtime restarts is to spin up a secondary server and have something like Server::Starter or HAProxy in front of your servers. Then, you tell one of your servers to gracefully spin down by processing all requests that are still in flight while already telling your proxy not to send new requests to that server instance. Once the instance has gone idle, you restart it and reconnect it to the proxy.

      I don't work with IIS so I don't know if there is a built-in mechanism for that, but as long as you have multiple (virutal) machines behind a proxy, you can use the technique even without server support.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://96480]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2016-06-25 01:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred method of making French fries (chips) is in a ...











    Results (322 votes). Check out past polls.