run_subroutine (version 1.0)

You can also look at:
#! /usr/bin/perl

##############################################################################
#
# SYNTAX: run_subroutine subroutine
#
# DESCRIPTION: This utility runs one of the major processing step subroutines.
#
# VERSION: 1.0
#
# HISTORY: 0.0 -> 1.0
# HISTORY: Subroutine error level set to "2". Before it was a null string.
#
##############################################################################
$SUBS = $ENV{"SUBS"};
$UTIL = $ENV{"UTIL"};
$JOBPAR = $ENV{"JOBPAR"};

$sub=$ARGV[0];
if ( $sub eq "" ) {
    exit 0;
}

$caller="process";
$stdout="$sub.stdout.log";
$stderr="$sub.stderr.log";

##########################################
# Check for a command in the job.par file
##########################################
chomp($command=`$UTIL/read_parfile $JOBPAR proc_command`);
chomp($restart=`$UTIL/read_parfile $JOBPAR stopped`);

if ( $command eq "quit" ) {
    ########
    # Quit
    ########
    if ( $restart ne "yes" ) {
        ###############################################################
        # Clear the command parameter, and set the "stopped" parameter
        ###############################################################
        `$UTIL/set_parameter $JOBPAR proc_command ""`;
        `$UTIL/set_parameter $JOBPAR stopped yes`;
        `$UTIL/milestone "Processing suspended"`;
        exit 0;
    }
}

######################
# Is this a restart?
######################
if ( $restart eq "yes" ) {
    ############################################################
    # This is a restart, so find out where we stopped.
    # If we stopped with this subroutine, then reset the
    # restart flags so we will resume with the next subroutine.
    ############################################################
    chomp($lastdone=`$UTIL/read_parfile $JOBPAR lastdone`);
    if ( $sub eq $lastdone ) {
        `$UTIL/set_parameter $JOBPAR stopped no`;
        `$UTIL/milestone "Processing resumed"`;
    }

    ###################################################################
    # If a stopped job was restarted, and we haven't yet reached the
    # last subroutine that was run, exit without doing anything.
    ###################################################################
    exit 0;
}

#####################
# Run the subroutine
#####################
`$SUBS/$sub >$stdout 2>$stderr`;
$code=$?;

###################
# Check for errors
###################
if ( -s "$stdout" or -s "$stderr" or $code != 0 ) {
    #####################
    # There was an error
    #####################
    `$UTIL/exception $caller 2 \\
       "Unexpected output from subroutine $sub. Exit code $code"`;

    if ( -s "$stdout" ) {
        ##############
        # Dump stdout
        ##############
        `$UTIL/log_entry "stdout from $sub"`;
        `$UTIL/file_to_log $stdout`;
    }

    if ( -s "$stderr" ) {
        ##############
        # Dump stderr
        ##############
        `$UTIL/log_entry "stderr from $sub"`;
        `$UTIL/file_to_log $stderr`;
    }
}

unlink $stdout;
unlink $stderr;


###############################################
# Mark this subroutine as the last thing done,
# in case we need to restart at this point.
###############################################
`$UTIL/set_parameter $JOBPAR lastdone "$sub"`;