I want to know how spring batch send job status (completed or failed) to shell script for that he launches his treatment.
Thank you.
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/
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.
Related
Hi all I think I am going insane. I have the following at the beginning of a bash script:
#!/bin/bash
while getopts i:o:c:p:g: flag
do
case "${flag}" in
g) genome=${OPTARG};;
p) poscol=${OPTARG};;
c) chromcol=${OPTARG};;
i) inf=${OPTARG};;
o) outf=${OPTARG};;
esac
done
echo ""
echo "This should be a list of the arguments:"
echo $inf
echo $outf
echo $poscol
echo $chromcol
echo $genome
echo "-----------------------"
Yet when I run a test of the code, eg:
bash Code.sh -i test.txt -o test.txt -c 0 -p 1 -g testgenome
bash returns:
This should be a list of the arguments:
-----------------------
I literally just copied this section of code from a script where the arguments work fine. Does anyone have any idea what could be going on here? Thanks in advance.
I'm trying to run a curl command within a bash script and I'm coming across the following error:
curl: (26) failed creating formpost data
When I echo the curl command and run that from the command line, that has no problems running. I've ensured there are read permissions for everyone on the file and the folder it's in, as well as using relative and absolute paths.
Here's a sample of the script:
#!/bin/bash
usage(){
echo "usage: <command> options: <u|p|f> "
}
while getopts u:p:f: option
do
case "${option}"
in
u) USERNAME=${OPTARG};;
p) PORT=${OPTARG};;
f) FILE=$OPTARG;;
esac
done
if [ $OPTIND -eq 1 ]; then
#echo "No options were passed";
usage
exit
fi
echo "Please enter your password: "
read PASSWORD
TEST="curl -u $USERNAME:$PASSWORD -F file=#\"$FILE\" -F force=true -F install=true http://localhost:$PORT/test.jsp"
echo $TEST
$TEST
echo $?
Thanks!
The code below which i have developed yesterday , takes either a file(x.txt) or space delimited numbers(434 435 436 437 ) as input and the pushes the data fron either case in an array and loops through.
Syntax: shtest.sh 434 435 436 or sh test.sh x.txt
MYFILE=$1 ;
OLDIFS=$IFS;
IFS=' ';
if [ -f "$MYFILE" ]; then
testCases=($(cat $MYFILE));
else
testCases=($#);
fi
for testCase in "${testCases[#]}"
do
echo "Running Testcases $testCase"
done
The way i want to do it to make it less error prone
I would like to actually have a flag while running the above script with a file as a paramenter
sh test.sh -f myFile.txt
or Can be run with -t flasg with numbers as the arguments
sh test.sh -t 434 435 436
if Both -f and -t exist, then i have to check and throw an error
ex: sh test.sh -f myFile.sh -t 343 435 435 or
sh test.sh -t 343 435 435 -f myFile.sh
I have started working on Shell Scripting just yesterday, and iam really short of how to do it syntactically
Finally , If we have flagged file or Flagged number arguments (No Unfalgged at all) , would the code below work or would that have any issues
Syntax:
sh test.sh -f myFile.txt #should work
sh test.sh -t 443 444 443 443 # should work
sh test.sh -f myFile.txt -t 443 444 443 443 # should fail
isFile=0;
isTest=0;
while getopts ":f:c:t:" opt; do
case $opt in
f)
echo "-f was triggered, Parameter: $OPTARG" >&2
testCases=($(cat $OPTARG));
isFile=1;
;;
t)
# testCases=($#);
multi+=("$OPTARG")
for val in "${multi[#]}"; do
echo " - $val"
done
;;
c)
echo "-c was triggered, Parameter: $OPTARG" >&2;
isTest=1;
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
# Exit if both flags are sent
if [ isTest==1 && isFile==1 ]; then
{
echo "Exiting";
}
fi
#if the flag is not avaiable, the store the orphan numbers (Command Line Arguments)
for testCase in "${testCases[#]}"
do
echo "Running Testcases $testCase"
done
Please advice and any help is appreciated.
Thanks
Tejas
After the while getopts loop, add:
shift $(($OPTIND - 1))
if [ $isFile = 0 ] && [ $# = 0 ]
then
echo "$0: must specify file (-f file) or numbers" >&2
exit 1
fi
I assume that you have isFile=0 before your loop. If you don't set it explicitly, you could inherit an environment variable that the user set accidentally. Always be careful that you don't accidentally use environment variables from the code that invokes your script.
I have a shell script that I use to launch some ROS launcher file (not that important for my question). My requirement is to use some arguments and therefore I am using getopts.
Basically, I am getting one file or a few, playing them with ROS and simultaneously recording all the files back to a single file. After that, if I have provided an argument -r or -c, I would like to run two additional operations (reindex and compress) on the recorded file. But it is required that the other process are done, before I can run the -r and -c. I am using the wait keyword, but I am not sure I really understand the flow. In other words, the playing and recording should be done and only then -r and -c should be run if provided as arguments.
Second question is related to how do I get or pass the same file that was outputted to these two functions (reindex and compress)?
So my desired format is:
./myscript -i file1 -i file2 -o /home/output -r -c
#!/bin/bash
OUTPUT_FILE_NAME="output_$(date +%Y.%m.%d--%H_%M_%S)"
usage="
$(basename "$0") [-i] [-o] [-r] [-c] [-h] -- to be done"
# Reset is necessary if getopts was used previously in the script.
OPTIND=1
while getopts ":i:orch:" opt; do
case $opt in
i ) echo "INPUT file - argument = $OPTARG"; input_files+=($OPTARG) ;;
o ) echo "OUTPUT dir - argument = $OPTARG"; output_dir=($OPTARG) ;;
r ) echo "REINDEX"; reindex ;;
c ) echo "COMPRESS"; compress ;;
h ) echo "$usage"
graceful_exit ;;
* ) echo "$usage"
exit 1
esac
done
# Shift off the options
shift $((OPTIND-1))
roslaunch myLauncher.launch &
echo "Number of loaded files: ${#input_files[#]}"
echo -n "FILES are:"
rosbag play ${input_files[#]} &
rosbag record -o $output_dir/$OUTPUT_FILE_NAME -a &
wait
function reindex{
rosbag reindex $output_dir/$OUTPUT_FILE_NAME
}
function compress{
rosbag reindex $output_dir/$OUTPUT_FILE_NAME
}
Thank you in advance!
You're very close to where you need to be — and using getopts puts you firmly on the correct track, too. Note whether or not you need to reindex or compress in the option parsing loop. Then, after the music has been played and the output file written, run the code from the functions if you need to:
#!/bin/bash
OUTPUT_FILE_NAME="output_$(date +%Y.%m.%d--%H_%M_%S)"
usage="$(basename "$0") [-i file] [-o file] [-r] [-c] [-h]"
input_files=() # Empty list of files/tracks
output_dir="." # Default output directory
r_flag="no" # No reindex by default
c_flag="no" # No compress by default
while getopts ":i:orch:" opt; do
case $opt in
(i) echo "INPUT file - argument = $OPTARG"; input_files+=("$OPTARG");;
(o) echo "OUTPUT dir - argument = $OPTARG"; output_dir="$OPTARG";;
(r) echo "REINDEX"; r_flag="yes";;
(c) echo "COMPRESS"; c_flag="yes";;
(h) echo "$usage" >&2; exit 0;;
(*) echo "$usage" >&2; exit 1;;
esac
done
# Shift off the options
shift $((OPTIND-1))
if [ $# != 0 ] || [ "${#input_files[#]}" = 0 ] ||
then
echo "$usage" >&2
exit 1
fi
roslaunch myLauncher.launch &
echo "Number of loaded files: ${#input_files[#]}"
echo -n "FILES are:"
rosbag play "${input_files[#]}" &
rosbag record -o "$output_dir/$OUTPUT_FILE_NAME" -a &
wait
if [ "$r_flag" = "yes" ]
then
rosbag reindex "$output_dir/$OUTPUT_FILE_NAME"
fi
if [ "$c_flag" = "yes" ]
then
rosbag compress "$output_dir/$OUTPUT_FILE_NAME"
fi
I didn't keep the functions since they didn't provide any value in the rewritten code.
#!/bin/bash
#!/bin/sh
# Need help
__help() { echo "$0 [ stop|start ]" 1>&2; exit 1; }
# Not enough args to run properly
[ $# -ne 1 ] && __help
# See what we're called with
case "$1" in
start) # Start sniffer as root, under a different argv[0] and make it drop rights
s=$(/usr/local/sbin/tcpdump -n -nn -f -q -i lo | awk 'END {print NR}')
echo "$s" > eppps_$(/bin/date +'%Y%m%d%H%M')
;;
stop) # End run, first "friendly", then strict:
/usr/bin/pkill -15 -f /usr/local/sbin/tcpdump >/dev/null 2>&1|| { sleep 3s; /usr/bin/pkill -9 -f /usr/local/sbin/tc$
;;
*) # Superfluous but show we only accept these args
__help
;;
esac
exit 0
This code runs perfectly on manual testing. But when i couple it with cron it just doesn't do anything. No output file is created.
My cron entries for the script looks like
http://postimage.org/image/1pztgd6xw/
It looks like you are not setting the working directory, so you may need to give an absolute path for the output file