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 - public ExitStatus processFile(String fileName, String workingDir) throws InterruptedException, IOException { boolean isWindows = System.getProperty("os.name").toLowerCase().startsWith("windows"); logger.info("OS windows {} :", isWindows); String command = String.format("tail -1 %s > input_footer.txt && head -n -1 %s > input_body.txt", fileName, fileName); logger.info("command {} and working dir{} :", command, workingDir); ProcessBuilder builder = new ProcessBuilder(); builder.command(bashPath, "-c", command); builder.directory(new File(workingDir)); Process process = builder.start(); StreamGobbler streamGobbler = new StreamGobbler(process.getInputStream(), System.out::println); Executors.newSingleThreadExecutor().submit(streamGobbler); int exitCode = process.waitFor(); ExitStatus status = exitCode == 0 ?
🌐
Mkyong
mkyong.com › home › spring batch › run spring batch job with commandlinejobrunner
Run Spring batch job with CommandLineJobRunner - Mkyong.com
July 24, 2013 - <!-- ... --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.5.1</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory> ${project.build.directory}/dependency-jars/ </outputDirectory> </configuration> </execution> </executions> </plugin> ... $ java -cp "target/dependency-jars/*:target/your-project.jar" org.springframework.batch.core.launch.support.CommandLineJobRunner spring/batch/jobs/job-read-files.xml readJob
🌐
Packtpub
subscription.packtpub.com › book › programming › 9781783985807 › 8 › ch08lvl1sec82 › executing-a-system-command
Executing a system command | Spring Cookbook
Access over 7,500 Programming & Development eBooks and videos to advance your IT skills. Enjoy unlimited access to over 100 new titles every month on the latest technologies and trends
🌐
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
🌐
What-When-How
what-when-how.com › Tutorial › topic-194n8n2 › Spring-Batch-121.html
Running a Job - Spring Batch - page 118
However, it isn't that simple. The org.springframework.batch.core.launch.JobLauncher interface, which · is responsible for the work of starting a job, can be implemented in a number of ways, exposing any · number of execution options (web, JMX, command line, and so on).
🌐
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.
Find elsewhere
🌐
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 - Batch developers use the Spring programming model: concentrate on business logic and let the framework take care of infrastructure. Clear separation of concerns between the infrastructure, the batch execution environment, and the batch application.
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="$( 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/

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 › html › configureJob.html
Overview :: Spring Batch Reference
The reference documentation is divided into several sections: · Background, usage scenarios, and general guidelines
🌐
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
🌐
Terasoluna-batch
terasoluna-batch.github.io › guideline › 5.0.0.RELEASE › en › Ch02_SpringBatchArchitecture.html
Spring Batch Architecture
Figure below shows a process flow from starting a Java process till starting a batch process. Process flow from starting a Java process till starting a batch process ... A shell script to start Java is generally described to start a Job defined on Spring Batch, along with starting a Java process.
🌐
Petri Kainulainen
petrikainulainen.net › home › blog › spring batch tutorial: introduction
Spring Batch Tutorial: Introduction - Petri Kainulainen
December 20, 2020 - Can identify the basic building blocks of a Spring Batch job. 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) and submitted for execution as a single unit.
🌐
Stack Overflow
stackoverflow.com › questions › 28818520 › spring-batch-command-line-execution
java - spring batch - command line execution - Stack Overflow
I have done the following downloaded sample spring batch project in to spring tool suite and followed the steps listed in the example . I was able to run the application inside the STS . but when i create the jar file using maven build --> clean install and try to execute the command I'm not that clear with the command to execute few options i used java -jar name.jar java -jar name.jar classpath:/launch-context.xml firstJob java org.springframework.batch.core.launch.support.commandlinejobrunner classpath:/launch-context.xml firstJob
🌐
Spring
docs.spring.io › spring-batch › reference › spring-batch-integration › launching-jobs-through-messages.html
Launching Batch Jobs through Messages :: Spring Batch Reference
For example, you may want to use the CommandLineJobOperator when invoking batch jobs by using a shell script. Alternatively, you can use the JobOperator directly (for example, when using Spring Batch as part of a web application). However, what about more complex use cases?