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 OverflowYou 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
You can do it n times (in a terminal) using a for loop this way:
for i in {1..10}; do java -jar run=$i; done
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/
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.
If you are already using SpringApplication from Spring Boot, why not finish the job and use @EnableAutoConfiguration as well, and also the Maven plugin (see for example this guide)? That way you will get something working pretty quickly and you can always add your own features later.
If the first argument to the CommandLineJobRunner is your @Configuration FQCN instead of a resource path, the ClassPathXmlApplicationContext constructor that's called from the CommandLineJobRunner's start() method will break.
int start(String jobPath, String jobIdentifier, String[] parameters, Set<String> opts) {
ConfigurableApplicationContext context = null;
try {
context = new ClassPathXmlApplicationContext(jobPath);
If you've already written a class with a main(), that replaces the CLJR, you shouldn't be passing CLJR as the class name in the command line. Pass that instead.
However, any command executed by ssh cannot be executed in the background permanently
But you can do this via service/systemctl
[Unit]
Description=xxxx
After=network.target syslog.target
[Service]
Type=simple
Restart=always
ExecStart=/usr/local/jdk/bin/java -jar /opt/xxx.jar --spring.profiles.active=prod
ExecStop=/uss/bin/kill -9 $MAINPID
ExecReload=/uss/bin/kill -9 $MAINPID && /usr/local/jdk/bin/java -jar /opt/xxx.jar --spring.profiles.active=prod
[Install]
WantedBy=multi-user.target
If you want to run in parallel cd /apps and java -jar you should use a single ampersand, not two: cd /apps & java -jar. However, the java -jar command will not work if it assumes it is run within the /apps directory.
More in general, command1 & command2 will run command1 and command2 independently, in parallel.
Note that your CI/CD tool (assuming you're talking about CI/CD pipelines) may support running tasks in parallel, so you could also try to solve this issue in the pipeline definition, if this makes sense in your case.