Tuesday, June 30, 2009

Perl Script to shutdown/startup all dbs on a server. oratab_shutdown_startup.pl

#!/usr/bin/perl

#########################################################################
# Shutdown or Startup all the databases from oratab file with a Y flag.
# pass shutdown or startup parameter
#########################################################################

use strict;
my $ORATAB;
my @fields;
my $SPOOL_FILE;
my $cmd;
my $result;


# Validate Input Parameter. STARTUP or SHUTDOWN
if ( lc($ARGV[0]) eq "startup") { $cmd = "startup" ; }
elsif ( lc($ARGV[0]) eq "shutdown") { $cmd = "shutdown immediate" ; }
else { print "Invalid Argument. Pass either shutdown or startup" . "\n"; exit; }
print $cmd . "\n";

# Get ORATAB file
if (-e '/etc/oratab') { $ORATAB='/etc/oratab'; }
elsif (-e '/var/opt/oracle/oratab') { $ORATAB='/var/opt/oracle/oratab'; }
else { print "oratab not found, please check manually", "\n"; exit; }

# Loop thu each entry in the oratab
open FILE, $ORATAB || die("Could not ORATAB!");
while () {

chomp; # chops off what is in $/
next if /^#/; # discard comments
next if /^\s+$/; # discard blank lines
@fields=split(/:/,$_); # Split the oratab entry into 3 fields
next if (@fields[2] ne "Y"); # discard if the 3rd variable is not Y
print "Processing " . @fields[0] . " " . @fields[1] . "\n";

$ENV{'ORACLE_SID'} = @fields[0];
$ENV{'ORACLE_HOME'} = @fields[1];
$SPOOL_FILE=@fields[0] . "_list.out";

$result = `@fields[1]/bin/sqlplus "/as sysdba" <$cmd
exit
END
`;

print $result,"\n";

}

close FILE;
exit;

No comments: