nohup in sh script not working - java

I'm having a problem with a sh script. I'm working on a sh script that allows me to execute a java script in background, and before executing it, check if it is currently running.
The code is here:
## START ##
start)
echo $" "
echo $"Checking for agents running, wait please..."
MI_PS=$(ps -ef|grep $JAR_NAME |awk '{print $2, $7}' |egrep -v "00:00:00" |awk '{print$1}')
sleep 1
for kill_process in $MY_PS; do
/bin/kill -9 $kill_process
done
# Verificamos que se ha realizado correctamente
VERIFY=$?
if [ "$?" = "0" ]; then
echo -n "Jar instance stopped correctly"
echo -ne "\033[0;32m [OK] \033[0m"
echo $" "
else
echo -n "Jar instance stopped incorrectly"
echo -ne "\033[0;31m [Error] \033[0m"
echo ""
echo "It is required to kill process "$MY_PS" manually."
echo "Ask your administrator"
echo $" "
fi
echo $" "
echo -n "Starting Jar"
echo $" "
nohup java -Dprops.dir=conf -jar func.jar &
echo $" "
echo -n "Jar running"
echo -ne "\033[0;32m [OK] \033[0m"
echo $" "
;;
The problem I'm having is that when setting the nohup ... & in order to execute in background jar file is not doing anything. If I write:
java -Dprops.dir=conf -jar func.jar
Then it works. My jar file is using a quartz library (scheduler library for Java), I don't know if some of you knows if there is any incompatibility between nohup and quartz...
Or maybe there is somekind of problem with setting JVM variables (-Dprops.dir=conf)
Thanks!

I figured out my problem, it turned out to not be related to Quartz at all. I was using SwingUtilities.invokeLater() for some cheap asynchronous tasks. For some reason the program was working only if I had xwindows running on my desktop, but would terminate with no error logged when I shut down my computer (also shutting down xwindows). The fix was to start the program with -Djava.awt.headless=true

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

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

What is the proper way to stop hiveserver2?

I've installed hive 0.14 on top of hadoop 2.6.0.
The setup mainly involved just extracting the tar.bin file.
I followed this guide to do the setup.
http://www.ishaanguliani.com/content/hive-0140-setup-ubuntu
I start hiveserver2 with a command line:
( $HIVE_HOME/bin/hiveserver2 &> hiveserver.log & )
Now, I am wondering what is the proper to stop hiveserver2. I can kill it but I doubt that provides a graceful exit.
write a small shell script to find hiveserver2 process and stop it. I used below shell script to stop hiveserver2 and hive metastore process. Hope this helps.
hive2_pid=`pgrep -f org.apache.hive.service.server.HiveServer2`
if [[ -n "$hive2_pid" ]]
then
echo "Found hivesevrer2 PID-- "$hive2_pid
kill $hive2_pid
# if process is still around, use kill -9
if ps -p $hive2_pid > /dev/null ; then
echo "Initial kill failed, killing with -9 "
kill -9 $hive2_pid
fi
echo "Hive server2 stopped successfully"
else
echo "Hiveserver2 process not found , HIveserver2 is not running !!!"
fi
meta_pid=`pgrep -f org.apache.hadoop.hive.metastore.HiveMetaStore`
if [[ -n "$meta_pid" ]]
then
echo "Found hivesevrer2 PID-- "$meta_pid
kill $meta_pid
# if process is still around, use kill -9
if ps -p $meta_pid > /dev/null ; then
echo "Initial kill failed, killing with -9 "
kill -9 $meta_pid
fi
echo "Hive metastore stopped successfully"
else
echo "Hive metastore process not found , Hive metastore is not running !!!"
fi
$ sudo service hive-server2 stop
refer this link : http://www.cloudera.com/documentation/archive/cdh/4-x/4-2-0/CDH4-Installation-Guide/cdh4ig_topic_18_8.html

Run a Java Application as a Service on Linux

I have written a Java server application that runs on a standard virtual hosted Linux solution. The application runs all the time listening for socket connections and creating new handlers for them. It is a server side implementation to a client-server application.
The way I start it is by including it in the start up rc.local script of the server. However once started I do not know how to access it to stop it and if I want to install an update, so I have to restart the server in order to restart the application.
On a windows PC, for this type of application I might create a windows service and then I can stop and start it as I want. Is there anything like that on a Linux box so that if I start this application I can stop it and restart it without doing a complete restart of the server.
My application is called WebServer.exe. It is started on server startup by including it in my rc.local as such:
java -jar /var/www/vhosts/myweb.com/phpserv/WebServer.jar &
I am a bit of a noob at Linux so any example would be appreciated with any posts. However I do have SSH, and full FTP access to the box to install any updates as well as access to a Plesk panel.
I wrote another simple wrapper here:
#!/bin/sh
SERVICE_NAME=MyService
PATH_TO_JAR=/usr/local/MyProject/MyJar.jar
PID_PATH_NAME=/tmp/MyService-pid
case $1 in
start)
echo "Starting $SERVICE_NAME ..."
if [ ! -f $PID_PATH_NAME ]; then
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is already running ..."
fi
;;
stop)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stoping ..."
kill $PID;
echo "$SERVICE_NAME stopped ..."
rm $PID_PATH_NAME
else
echo "$SERVICE_NAME is not running ..."
fi
;;
restart)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stopping ...";
kill $PID;
echo "$SERVICE_NAME stopped ...";
rm $PID_PATH_NAME
echo "$SERVICE_NAME starting ..."
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is not running ..."
fi
;;
esac
You can follow a full tutorial for init.d here and for systemd (ubuntu 16+) here
If you need the output log replace the 2
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
lines for
nohup java -jar $PATH_TO_JAR >> myService.out 2>&1&
A simple solution is to create a script start.sh that runs Java through nohup and then stores the PID to a file:
nohup java -jar myapplication.jar > log.txt 2> errors.txt < /dev/null &
PID=$!
echo $PID > pid.txt
Then your stop script stop.sh would read the PID from the file and kill the application:
PID=$(cat pid.txt)
kill $PID
Of course I've left out some details, like checking whether the process exists and removing pid.txt if you're done.
Linux service init script are stored into /etc/init.d. You can copy and customize /etc/init.d/skeleton file, and then call
service [yourservice] start|stop|restart
see http://www.ralfebert.de/blog/java/debian_daemon/. Its for Debian (so, Ubuntu as well) but fit more distribution.
Maybe not the best dev-ops solution, but good for the general use of a server for a lan party or similar.
Use screen to run your server in and then detach before logging out, this will keep the process running, you can then re-attach at any point.
Workflow:
Start a screen: screen
Start your server: java -jar minecraft-server.jar
Detach by pressing: Ctl-a, d
Re-attach: screen -r
More info here: https://www.gnu.org/software/screen/manual/screen.html
Another alternative, which is also quite popular is the Java Service Wrapper. This is also quite popular around the OSS community.
Referring to Spring Boot application as a Service as well, I would go for the systemd version, since it's the easiest, least verbose, and best integrated into modern distros (and even the not-so-modern ones like CentOS 7.x).
The easiest way is to use supervisord. Please see full details here: http://supervisord.org/
More info:
https://askubuntu.com/questions/779830/running-an-executable-jar-file-when-the-system-starts/852485#852485
https://www.digitalocean.com/community/tutorials/how-to-install-and-manage-supervisor-on-ubuntu-and-debian-vps
Here is a sample shell script (make sure you replace the MATH name with the name of the your application):
#!/bin/bash
### BEGIN INIT INFO
# Provides: MATH
# Required-Start: $java
# Required-Stop: $java
# Short-Description: Start and stop MATH service.
# Description: -
# Date-Creation: -
# Date-Last-Modification: -
# Author: -
### END INIT INFO
# Variables
PGREP=/usr/bin/pgrep
JAVA=/usr/bin/java
ZERO=0
# Start the MATH
start() {
echo "Starting MATH..."
#Verify if the service is running
$PGREP -f MATH > /dev/null
VERIFIER=$?
if [ $ZERO = $VERIFIER ]
then
echo "The service is already running"
else
#Run the jar file MATH service
$JAVA -jar /opt/MATH/MATH.jar > /dev/null 2>&1 &
#sleep time before the service verification
sleep 10
#Verify if the service is running
$PGREP -f MATH > /dev/null
VERIFIER=$?
if [ $ZERO = $VERIFIER ]
then
echo "Service was successfully started"
else
echo "Failed to start service"
fi
fi
echo
}
# Stop the MATH
stop() {
echo "Stopping MATH..."
#Verify if the service is running
$PGREP -f MATH > /dev/null
VERIFIER=$?
if [ $ZERO = $VERIFIER ]
then
#Kill the pid of java with the service name
kill -9 $($PGREP -f MATH)
#Sleep time before the service verification
sleep 10
#Verify if the service is running
$PGREP -f MATH > /dev/null
VERIFIER=$?
if [ $ZERO = $VERIFIER ]
then
echo "Failed to stop service"
else
echo "Service was successfully stopped"
fi
else
echo "The service is already stopped"
fi
echo
}
# Verify the status of MATH
status() {
echo "Checking status of MATH..."
#Verify if the service is running
$PGREP -f MATH > /dev/null
VERIFIER=$?
if [ $ZERO = $VERIFIER ]
then
echo "Service is running"
else
echo "Service is stopped"
fi
echo
}
# Main logic
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart|reload)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload}"
exit 1
esac
exit 0
From Spring Boot application as a Service, I can recommend the Python-based supervisord application. See that stack overflow question for more information. It's really straightforward to set up.
Other answers do a good job giving custom scripts and setups depending on your platform. In addition to those, here are the mature, special purpose programs that I know of:
JSW from TanukiSoftware
YAJSW is an open source clone from the above. It is written in Java, and it is a nanny process that manages the child process (your code) according to configurations. Works on windows / linux.
JSVC is a native application. Its also a nanny process, but it invokes your child application through the JNI, rather than as a subprocess.
You can use Thrift server or JMX to communicate with your Java service.
From Spring Boot Reference Guide
Installation as an init.d service (System V)
Simply symlink the jar to init.d to support the standard start, stop, restart and status commands.
Assuming that you have a Spring Boot application installed in /var/myapp, to install a Spring Boot application as an init.d service simply create a symlink:
$ sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp
Once installed, you can start and stop the service in the usual way. For example, on a Debian based system:
$ service myapp start
If your application fails to start, check the log file written to /var/log/<appname>.log for errors.
Continue reading to know how to secure a deployed service.
After doing as written I've discovered that my service fails to start with this error message in logs: start-stop-daemon: unrecognized option --no-close. And I've managed to fix it by creating a config file /var/myapp/myapp.conf with the following content
USE_START_STOP_DAEMON=false
It is possible to run the war as a Linux service, and you may want to force in your pom.xml file before packaging, as some distros may not recognize in auto mode. To do it, add the following property inside of spring-boot-maven-plugin plugin.
<embeddedLaunchScriptProperties>
<mode>service</mode>
</embeddedLaunchScriptProperties>
Next, setup your init.d with:
ln -s myapp.war /etc/init.d/myapp
and you will be able to run
service myapp start|stop|restart
There are many other options that you can find in Spring Boot documentation, including Windows service.
Im having Netty java application and I want to run it as a service with systemd. Unfortunately application stops no matter of what Type I'm using. At the end I've wrapped java start in screen. Here are the config files:
service
[Unit]
Description=Netty service
After=network.target
[Service]
User=user
Type=forking
WorkingDirectory=/home/user/app
ExecStart=/home/user/app/start.sh
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
start
#!/bin/sh
/usr/bin/screen -L -dmS netty_app java -cp app.jar classPath
from that point you can use systemctl [start|stop|status] service.
To run Java code as daemon (service) you can write JNI based stub.
http://jnicookbook.owsiak.org/recipe-no-022/
for a sample code that is based on JNI. In this case you daemonize the code that was started as Java and main loop is executed in C. But it is also possible to put main, daemon's, service loop inside Java.
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo029
Have fun with JNI!
However once started I don't know how to access it to stop it
You can write a simple stop script that greps for your java process, extracts the PID and calls kill on it. It's not fancy, but it's straight forward.
Something like that may be of help as a start:
#!/bin/bash
PID = ps ax | grep "name of your app" | cut -d ' ' -f 1
kill $PID

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