#!/usr/local/bin/perl # # File: bail-out.pl # Abstract: Simulate the various ways a Unix process can exit or die. # Author: Bill.Costa@unh.edu -- CIS Network Information Services # Date: 12-OCT-2000 # # I created this program for testing exit and error handling of # scripts that I write. This is suppose to cover all the ways I # could find that a Unix process can become an ex-process. If you # know of any other ways -- other than the halting the CPU or killing # shell, let me know! # #============================================================================== my $ownName = $0; # So we can say who we are. $ownName =~ s{.*/}{}; my $op; # Method of exit. my $code; # Code to use with that method. my $usrMsg; # Print this to STDOUT or STDERR. #------------------------------------------------------------------------------ # Subroutines --------------------------------------------------------------- #------------------------------------------------------------------------------ sub showSigCodes () # These are different for different flavors of Unix. For the codes # on any given system, try any one of: # # kill -l # trap -l # man 4 signal # more /usr/include/signal.h # # These are for Redhat Linux. { print(STDERR < " ); $op = ; chomp($op); } $op =~ s/^\s+|\s+$//g; # Trim leading and trailing spaces. die("! $ownName: use one of [ceoi] for first arg\n") if ($op !~ m/[ceoi]/); #-------------------------------+ # Get the code/signal to use. | #-------------------------------+ if (not $code) { showSigCodes() if ($op eq "i"); print(STDERR "\t\t", ($op eq "i") ? "signal> " : "status> "); $code = ; chomp($code); $code =~ s/^\s+|\s+$//g; } die("! $ownName: '$code' is not an integer\n") if ($code !~ m/^[+-]?\d+$/); #-------------------------------+ # Say good night Dick. | #-------------------------------+ if ($op eq "i") { print(STDERR "$ownName: killing myself with '$code' signal\n"); kill($code,0); print(STDERR "Process has been resurrected after signal!\n"); } elsif ($op =~ m/[ceo]/) { if ($op eq "e") { print(STDERR "$ownName: exiting with status '$code'\n$usrMsg\n"); } elsif ($op eq "o") { print("$ownName: exiting with status '$code'\n$usrMsg\n"); } exit($code); print(STDERR "Huh!?\n"); } warn("! $ownName: exiting with 0 status\n"); exit 0; #==[ EOF: bail-out.pl ]========================================================