Monoem Youneb
Monoem Youneb

Reputation: 95

Spring batch Shell Script pass status

I want to know how spring batch send job status (completed or failed) to shell script for that he launches his treatment. Thank you.

Upvotes: 0

Views: 1287

Answers (2)

Nghia Do
Nghia Do

Reputation: 2658

I have an example which is to capture an exit code from spring batch called from Shell script.

JOB_RET=$?.

Hope it helps.

    #!/bin/bash
#
#  Launch Spring Batch
#
#Get the absolute path to the folder containing the folder this script is located in
BIN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
HOME_DIR=$(dirname $BIN_DIR)
BATCH_HOME=$HOME_DIR
RUN_ID=$(date +"%Y-%m-%d %H:%M:%S")
LOG4J_CONF=file:$HOME_DIR/config/log4j.xml

function show_help {
    echo "usage:  $BASH_SOURCE -o <options> -d <month> -y <year>"
    echo "             where command is one of the following: "
    echo "                   options                    - Options (such as autoLogin|autoLogOut)."
    echo "                   month                      - Enter a month as Job param."
    echo "                   year                       - Enter a year as Job param."
    exit 1
}

# Read command line options
OPTIND=1         # Reset in case getopts has been used previously in the shell.
while getopts "ho:d:y:" opt; do
    case "$opt" in
    h)
        show_help
        exit 0
        ;;
    d)  options=$OPTARG
        ;;
    s)  month=$OPTARG
        ;;
    o)  year=$OPTARG
        ;;
    esac
done

JOB_PARAMS="options=$options month=$month year=$year"

#Run the job
$JAVA_HOME -cp "$(echo $HOME_DIR/lib/*.jar | tr ' ' ':') \
        -Djava.security.properties==$JAVA_SECURITY_FILE \
        -Dlog4j.configuration=$LOG4J_CONF \
        -DBATCH_HOME=$HOME_DIR -d64 \
               org.springframework.batch.core.launch.support.CommandLineJobRunner batch-jobs.xml <job-name> run.id="$RUN_ID" ${JOB_PARAMS} > $HOME_DIR/logs/stdout.log 2>&1

JOB_RET=$?
echo "Job returns $JOB_RET" >> $HOME_DIR/logs/stdout.log
exit $JOB_RET

https://bigzidane.wordpress.com/2016/06/26/launch-spring-batch-job-from-shell-script-sh/

Upvotes: 2

Mahmoud Ben Hassine
Mahmoud Ben Hassine

Reputation: 31600

The ExitCodeMapper is what you are looking for. It maps the exit code of your job to an integer which will be the exit code of the JVM running your job.

If you run your job from a shell script, this exit code will be the value returned by your script.

Upvotes: 0

Related Questions