Does java daemonize itself? - java

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=$?

Related

War file automated deployment

So I deploy war files on a Linux box using java automatically
and the same commands are always used
ps -ef | grep java
kill - 9 (java process)
java -jar ROOT.war &>/dev/null &
However, I get different versions for it so like
ROOT_1.0.2.war
ROOT_1.0.3.war
ROOT_1.0.4.war
ROOT_1.0.5.war
I want the script to see the new .war and deploy it automatically
and keep it deployed which is why I use &>/dev/null & so it runs in the till it is killed again till the new version is put in that directory
echo Enter the name of the process you want to kill eg ROOT.war?
enter code here
read process
##Kill selected process
file="$process"
if [ -f "$file" ];
then
pkill -9 -f $process
echo process stopped >> satrixWar.txt
sleep 3s
## Start Up selected process
echo Enter the name of the process you want to start
read process2
java -jar $process2 &>/dev/null &
echo process starting up>> satrixWar.txt
else
echo "Process $process does not exist" >&2
fi
##Confirm new proess is up
echo What is currently installed >> satrixWar.txt
ps -ef | grep $process2 >> satrixWar.txt
mail -a text file path -s "name"
"email.com" < /dev/null
rm -rf War.txt

How can I do Java/Kotlin to CLI?

I have a Java/Kotlin program, which gets arguments from String[] args, and I need to make it executable from everywhere from console, without prefixing it with java word. Like only the name of the program and its arguments. How can I do it?
Like git or heroku:
name command
It depends on your operating system, but on Unix the following script would work:
#!/bin/sh
MYSELF=`which "$0" 2>/dev/null`
[ $? -gt 0 -a -f "$0" ] && MYSELF="./$0"
java=java
if test -n "$JAVA_HOME"; then
java="$JAVA_HOME/bin/java"
fi
exec "$java" $java_args -jar ${MYSELF}.jar "$#"
exit 1
You need to append this script at the start of your jar using cat, like the following
cat script.sh my.jar > my-program
And move my-program to some dir in your $PATH. After that, you'll be able to call my-program as usual program.

individually process control java or linux script

this script will read a text file that we have already filled in as process-list and check if the process names in it are working individually. How can I write Java code or do it with linux script?
while read line;
do
check = `ps -ef|grep -i $line|grep -v grep |wc -l`
if [ $check -eq 0 ]
then
$line is not running
fi
done<yourTextFile
Does this answer your query.?

Starting Screen with a Command

I need to be able to start up a screen without connecting to it, but it also needs to run my start.sh script which contains the java line to start Minecraft.
screen -d -m new3 -c start.sh
Is what I've been trying to use, but it never runs start.sh
In a snippet of code I found on line it seems to do what I want but I need some help
mc_start() {
cd $MCPATH
as_user "cd $MCPATH && screen -dmS $SCREEN $INVOCATION"
#
# Waiting for the server to start
#
seconds=0
until ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
do
sleep 1
seconds=$seconds+1
if [[ $seconds -eq 5 ]]
then
echo "Still not running, waiting a while longer..."
fi
if [[ $seconds -ge 120 ]]
then
echo "Failed to start, aborting."
exit 1
fi
done
echo "$SERVICE is running."
}
I think this is because your command is wrong. I'm assuming that you want to create a new session named new3 and detach from that
screen -d -m -S new3 ~/start.sh
Afterwards you can run the following command to connect back to your session.
screen -R new3

Start java process at boot and auto-restart on death

I have two requirements for my Java app. If it dies, restart it. If the server reboots, restart it - simple enough. Using the answer here I have a script that will restart when the java application dies.
#!/bin/bash
until java -Xms256m -Xmx768m -jar MyApp.jar; do
echo "MyApp crashed with exit code $?. Respawning... " >&2
sleep 5
done
I can run this with "nohup restart_script.sh &" and it will run all day long without issue. Now for the startup requirement. I took the /etc/init.d/crond script and replaced the crond binary with my script but it hangs on startup.
#!/bin/bash
#
# Init file for my application.
#
. /etc/init.d/functions
MYAPP=restart_script.sh
PID_FILE=/var/run/myapp.pid
start(){
echo -n "Starting My App"
daemon --user appuser $MYAPP
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/myapp
return $RETVAL
}
stop(){
echo -n "Stopping my application"
killproc $MYAPP
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/myapp
return $RETVAL
}
...
case "$1" in
start)
start
;;
stop)
stop
;;
...
esac
When I run /sbin/service myapp start the script starts but hangs the console. I have tried "daemon --user appuser nohup $MYAPP &" and I am immediately returned to the prompt without any [OK] indication and when I do a ps, I still see that the init is hung. Any ideas how to call a script within the init script and get it to return properly?
Thanks,
Greg
The daemon function on my machine (old RedHat) does not return until the executed program returns. So you are going to need to have your little utility script do the forking.
Try writing your utility like this:
#!/bin/bash
(
until java -Xms256m -Xmx768m -jar MyApp.jar; do
echo "MyApp crashed with exit code $?. Respawning... " >&2
sleep 5
done
) &
How this works. Putting a command in parentheses starts code running in a new process. You put the process in the background so the original process will return without waiting for it.
You need a java service wrapper, here is a very good one... tanuki
I mean to say, you don't need to reinvent the wheel, there are tools out there..

Categories