http://www.perlmonks.org?node_id=74672
Category: NT Admin
Author/Contact Info idnopheq
Description: shutdown -- shut down system, change system state

shutdown is executed by the super-user to change the state of the machine.

By default, shutdown brings the system to a state ready for power off.

Before starting to shut down services and killing processes, shutdown sends a warning message via GUI pop-up with the warning message "The systemis shutting down. Please save all work in progress and log off. Any unsaved changes will be lost. This shutdown was initiated by ..." The bottom of the window pop-up contains the custom message included in the shutdown command. If the string contains more than one word, it should be contained within single (') or double (") quotation marks.

The warning message provides output detailing the time remaining before shutdown begins. See EXAMPLES .

No warning is sent to Remote Access Service (RAS) users or users connected via rcmd or telnet.

#!/usr/local/bin/perl -w
#-*-perl-*-
#
#
#

use strict;
use Win32;
use Getopt::Std;
use vars qw ( %Option );

my ($VERSION) = '$Revision: 1.0 $' =~ /([.\d]+)/;

my $warnings = 0;

# Print a usuage message on a unknown option.

$SIG {__WARN__} = sub {
    if (substr ($_ [0], 0, 14) eq "Unknown option") {die "Usage"};
    require File::Basename;
    $0 = File::Basename::basename ($0);
    $warnings = 1;
    warn "$0: @_";
};

$SIG {__DIE__} = sub {
    require File::Basename;
    $0 = File::Basename::basename ($0);
    if (substr ($_ [0], 0,  5) eq "Usage") {
        die <<EOF;
$0 (NT Perl bin utils) $VERSION
$0 [ -a ] | [ -y ] [ -g grace-period ] [ -i init-state ] [ message ]
EOF
    }
    die "$0: @_";
};

# Get the options.

getopts ( 
     'hayi:g:',
      \%Option 
    );                    # -h, -y & -a take no option, -i & -g take a
+rg

die "Usage" unless scalar keys %Option > 0;

my $Server = "";

if ( $Option{'a'} ) {
  Win32::AbortSystemShutdown (
                  $Server
                 ) 
      or die "$^E\n";
  exit;
}

my $Message = $ARGV[0];
my $Timeout = $Option{'g'} || "60";
my $ForceClose = $Option{'y'} || "0";
my $Reboot = $Option{'i'} || "5";

Win32::InitiateSystemShutdown (
                   $Server,
                   $Message,
                   $Timeout,
                   $ForceClose,
                   $Reboot - 5
                  ) 
  or die "$^E\n";

=pod

=head1 NAME

B<shutdown> -- shut down system, change system state

=head1 SYNOPSIS

B<shutdown> [ -a ]

B<shutdown> [ -y ] [ -g I<grace-period> ] [ -i I<init-state> ] [ I<mes
+sage> ]

=head1 DESCRIPTION

B<shutdown> is executed by the super-user to change the state of the m
+achine.

By default, B<shutdown> brings the system to a state ready for power o
+ff.

Before starting to shut down services and killing processes, B<shutdow
+n> sends a warning message via GUI pop-up with the warning message "T
+he systemis shutting down.  Please save all work in progress and log 
+off.  Any unsaved changes will be lost.  This shutdown was initiated 
+by ..."  The bottom of the window pop-up contains the custom message 
+included in the B<shutdown> command.  If the string contains more tha
+n one word, it should be contained within single (') or double (") qu
+otation marks.

The warning message provides output detailing the time remaining befor
+e shutdown begins. See EXAMPLES .

No warning is sent to Remote Access Service (RAS) users or users conne
+cted via B<rcmd> or B<telnet>.

=head2 System state definitions

=over 4

=item state 5

Shut the machine down so that it is safe to remove the power. Have the
+ machine remove power, if possible.

=item state 6

Stop the operating system and reboot without powering off.

=back

=head2 OPTIONS

=over 4

=item -a

Abort the B<shutdown> command.

=item -y

Pre-answer the confirmation question so the command can be run without
+ user intervention.  Forces running applications closed without user 
+intervention.  B<WARNING> - does not save data.

=item -g grace-period

Allow the super-user to change the number of seconds from the 60-secon
+d default.

=item -i init-state

If there are warnings, init-state specifies the state init is to be in
+. By default, system state 5 (shutdown) is used.

=item -h

Display syntax.

=back

=head1 EXAMPLES

In the following example, B<shutdown> is being executed on host foo an
+d is scheduled in 120 seconds

C:\> shutdown -i 5 -g 120 "===== disk replacement ====="

The following example aborts the above.

C:\> shutdown -a

=head1 ENVIRONMENT

The working of B<shutdown> is not influenced by any environment variab
+les.

=head1 BUGS

B<shutdown> suffers from no known bugs at this time.

=head1 STANDARDS

It does not make sense to talk about standards in a B<shutdown> manual
+ page.

=head1 REVISION HISTORY

    shutdown
    Revision 1.0  2000/06/22 06:44:57  idnopheq
    Initial revision

=head1 AUTHOR

The Perl implementation of B<shutdown> was written by Dexter Coffin, I
+<idnopheq@home.com>.

=head1 COPYRIGHT and LICENSE

This program is copyright by Dexter Coffin 2000.

This program is free and open software. You may use, copy, modify, dis
+tribute,
and sell this program (and any modified variants) in any way you wish,
provided you do not restrict others from doing the same.

=head1 SEE ALSO

=for html
<a href="reboot.html">reboot</a><p>

=head1 NEXT TOPIC

=cut