Does java daemonize itself?

I have this code in a java wrapper shell script, written by a third party:

TEMPFILE=$(mktemp java-wrapper-XXXXX)
"$@" | awk -v t=${TEMPFILE} '/unable to fund java heap account/ {print 1 > t} {print}'
RC=$?

if [ 0 -eq ${RC} -o ! -s ${TEMPFILE} ]; then
  exit
fi

$@ is supposed to hold the java command line, along with the arguments. The logic here is to:

  • rerun the java command if it fails with heap issues, after invoking another script to defragment memory
  • do nothing and let the java process run in the background, otherwise

In "$@" | awk ..., java is being called in the foreground. Does java daemonize itself if it bootstraps successfully? That doesn't seem to be making sense to me. Unless there is a memory issue, this code would lead to java process running normally with its output being piped to awk, isn't it?

Please help me understand this. I welcome any suggestions to improve the logic. Please ignore the uppercase variables and other issues that can be found through shellcheck.


Here is the complete script:

#!/bin/bash

# Java Wrapper takes java command as input
# and runs java. If java fails due to zing memory
# it attempts to run get2Mpages.sh (az_fragger binary) for
# the given Xmx and them runs java again

set -o pipefail

BASE=$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)
NAME=$(basename "${BASH_SOURCE[0]}")

GET_2MPAGES=${BASE}/get2Mpages.sh

fail() {
   echo "ERROR: $@" >&2
   echo "Usage: $NAME java [<args>...] -XmxNNN [<args>...]" >&2
   exit 1
}

[ $# -gt 0 ] || fail "No command specified"

# extract the Xmx value
XMX=$(echo "$@" | sed -n 's/.*-Xmx\([0-9]*.\).*/\1/p')
[ -n "${XMX}" ] || fail "Unable to extract Xmx argument from the command-line"

trap on_exit SIGTERM SIGQUIT EXIT

on_exit() {
  rm -f "${TEMPFILE}"
  exit ${RC}
}

TEMPFILE=$(mktemp java-wrapper-XXXXX)

"$@" | awk -v t=${TEMPFILE} '/unable to fund java heap account/ {print 1 > t} {print}'
RC=$?

if [ 0 -eq ${RC} -o ! -s ${TEMPFILE} ]; then
  exit
fi

# OOM Detected
cat << EOF >&2
Info: Failed to run JAVA due to insufficient 2MB pages
Info: Now running $GET_2MPAGES ${XMX}
EOF

${GET_2MPAGES} ${XMX} && {
  echo "Info: attempting to run JAVA again" >&2
  echo
  "$@"
}
RC=$?