You need an incrementer for this as usual.

<bean id="simpleIncrementer"
class="org.springframework.batch.core.launch.support.RunIdIncrementer"/>
<job id="myJob" incrementer="simpleIncrementer">
</job>

The trick for this incrementer to work with CommandLineJobRunner is adding the -next parameter when running the task.

-next: (optional) to start the next in a sequence according to the JobParametersIncrementer in the Job

Something like this:

java –jar myjob.jar jobs/myjob.xml myjob -next
Answer from Serkan Arıkuşu on Stack Overflow
🌐
b.i.g Z.i.d.a.n.e
bigzidane.wordpress.com › 2016 › 06 › 26 › launch-spring-batch-job-from-shell-script-sh
Launch Spring Batch job from Shell Script (.sh) | b.i.g Z.i.d.a.n.e
June 26, 2016 - The script is saved as runBatchJob.sh and 3 optional parameter (options, moth and year) The command line is ./runBatchJob -o -m -y ./runBatchJob -o autoLo…
🌐
Spring
docs.spring.io › spring-batch › docs › 1.0.x › spring-batch-docs › reference › html › execution.html
Chapter 4. Configuring and Executing A Job
However, because most people are familiar with shell scripts, this example will focus on them. Because the script launching the job must kick off a Java Virtual Machine, there needs to be a class with a main method to act as the primary entry point. Spring Batch provides an implementation that serves just this purpose: CommandLineJobRunner.
🌐
Stack Overflow
stackoverflow.com › questions › 62333601 › use-shell-command-in-spring-batch-systemcommandtasklet
java - Use shell Command in Spring Batch SystemCommandTasklet - Stack Overflow
June 11, 2020 - I wanted to run this command in spring batch systemCommandTasklet but it is not working. tail -1 file1.txt > input_footer.txt && head -n -1 file1.txt > input_body.txt Code:
Top answer
1 of 2
2

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="( 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/

2 of 2
0

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.

🌐
Spring
docs.spring.io › spring-batch › reference › job › running.html
Running a Job :: Spring Batch Reference
However, because most people are familiar with shell scripts, this example focuses on them. Because the script launching the job must kick off a Java Virtual Machine, there needs to be a class with a main method to act as the primary entry point. Spring Batch provides an implementation that serves this purpose: CommandLineJobOperator.
🌐
GitHub
github.com › spring-projects › spring-batch › issues › 3273
Add sample script to allow user to launch job via dos bat file and Unix shell script [BATCH-306] · Issue #3273 · spring-projects/spring-batch
January 22, 2008 - Hilda Lu opened BATCH-306 and commented The current sample job only allows user to launch job within eclipse. It will be very help to provide developer sample dos and shell script. Attachments: batchJobRunner.sh (2.96 kB)
Author   spring-projects
🌐
Packtpub
subscription.packtpub.com › book › programming › 9781783985807 › 8 › ch08lvl1sec82 › executing-a-system-command
8. Running Batch Jobs | SPRING COOKBOOK
A step can consist of just an execution of a system command. Spring Batch provides a convenient class for this, SystemCommandTasklet.
Find elsewhere
🌐
What-When-How
what-when-how.com › Tutorial › topic-194n8n2 › Spring-Batch-121.html
Running a Job - Spring Batch - page 118
application. If you want to run a stand-alone Java program, you either create an executable jar file or call · the class directly. In either case you might write a shell script to launch the process. However, running a batch job is different.
🌐
Spring
docs.spring.io › spring-batch › reference › html › configureJob.html
Overview :: Spring Batch Reference
The reference documentation is divided into several sections: · Background, usage scenarios, and general guidelines
🌐
O'Reilly
oreilly.com › library › view › pro-spring-batch › 9781430234524 › ch06.html
6. Running a Job - Pro Spring Batch [Book]
July 11, 2011 - To run the application, you start the container, which starts the application. If you want to run a stand-alone Java program, you either create an executable jar file or call the class directly. In either case you might write a shell script to launch the process. However, running a batch job is ...
Author   Michael T. Minella
Published   2011
Pages   502
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › enterprise java › spring › batch
Spring Batch CommandLineJobRunner Example - Java Code Geeks
February 13, 2019 - Let us dive into spring batch with a simple example of a tasklet and see how we can leverage CommandLineJobRunner to run our job. The main use of CommandLineJobRunner is to run our Spring Batch jobs via Shell script.
🌐
Stack Overflow
stackoverflow.com › questions › 42960797 › how-can-we-send-spring-xd-shell-commands-from-batch-job
How can we send Spring-xd shell commands from Batch Job - Stack Overflow
March 22, 2017 - Im trying to relaunch a JobB by running JobA. I was trying to use jobOperater.restart() and I was unsuccessful. I was able to restart the jobB from the xd-shell by "job execution restart --id 0". C...
🌐
Petri Kainulainen
petrikainulainen.net › home › blog › spring batch tutorial: introduction
Spring Batch Tutorial: Introduction - Petri Kainulainen
December 20, 2020 - Let's start by defining the term batch job. ... A batch job is a computer program or set of programs processed in batch mode. This means that a sequence of commands to be executed by the operating system is listed in a file (often called a batch file, command file, or shell script...
🌐
Medium
medium.com › @ndongotonuxsamb › introduction-to-spring-batch-41b5e11fd5f5
Introduction to Spring Batch. Now we tend to go more towards… | by Ndongo Tonux Samb | Medium
June 8, 2019 - In my mind I thought to make a shell script or develop a program from scratch to do the process (recover, loop, read, process, transform, insert, …). But within this mode of treatment the risk is enormous. This may take longer, error handling is to be expected, manage rollbacks , not reliable because of the possibility of having an OutOfMemoryError, …