System.exit return code isn't detected by bash

2019-02-23 07:35发布

struggling with this for an hour... java code:

ULogger.info("throwing out 666!");
System.exit(666);

bash wrapper:

eval ${COMMAND_TO_RUN}
ret_code=$?
printf "error code : [%d]" ${ret_code}

output:

[2012-11-30 15:20:12,971][INFO ] throwing out 666!
error code : [0]

what's the deal here? Thanks...

[EDIT]

The ${COMMAND_TO_RUN} is

((java -Xmx9000m -Dtoday_nix=20121128 -cp "/usr/lib/hadoop/conf" com.paypal.risk.ars.linking.task_fw.BaseRunnableProcess 3>&1 1>&2 2>&3) | tee /dev/tty) > batches_errors.log

2条回答
聊天终结者
2楼-- · 2019-02-23 08:22

Your problem is in your COMMAND_TO_RUN:

((java -Xmx9000m -Dtoday_nix=20121128 -cp "/usr/lib/hadoop/conf" com.paypal.risk.ars.linking.task_fw.BaseRunnableProcess 3>&1 1>&2 2>&3) | tee /dev/tty) > batches_errors.log

The last program called is tee, which will exit with status 0, overriding the exit value of java.

You can use $PIPESTATUS, which is an array of return values in the pipe.

For example:

$ cat nonexistantFile | echo ; echo "e: $? p: ${PIPESTATUS[@]}"

Expected output:

e: 0 p: 1 0

cat will fail (exit code 1), echo will succeed (exit code 0). $? will be 0. ${PIPESTATUS[0]} will contain the exit code for cat (1) and ${PIPESTATUS[1]} the one for echo (0).

查看更多
做个烂人
3楼-- · 2019-02-23 08:26

This is because $? is almost certainly giving you the return code of eval, which is succeeding here. Why are you including the eval call in there? Just call the COMMAND_TO_RUN

查看更多
登录 后发表回答