Why would my spring boot application will shut down automatically? - java

update:
I have followed spring boot deployment for Installation as an init.d Service (System V). I start my application successfully.But, after one day.My application closed again......Is there any ways to help me ?
I hava one spring boot application which names my.jar. I put it into my ubuntu server(20.04) and use the command of nohup java -jar my.jar &.In the first few hours, my application is in good condition.But,after one or two days,it will shut down automatically.
I have seen the log of my application which don't recorded any error and saved the last correct log before exiting
The current situation is my application is very simple and have only a small number of visits.
According to my guess,it is seems that ubuntu kill my process for inactivity?
My scripts are as follow,
stop.sh:
#!/bin/bash
PID=$(ps -ef | grep centre-0.0.1-SNAPSHOT.jar | grep -v grep | awk '{ print $2 }')
if [ -z "$PID" ]
then
echo Application is already stopped
else
echo kill $PID
kill $PID
fi
run.sh:
#!/bin/bash
echo stop application
source stop.sh
echo start application
source start.sh
start.sh:
#!/bin/bash
nohup java -jar centre-0.0.1-SNAPSHOT.jar &
I need my application will run all the time.
Is there any ways to resolve the issue?

nohup is more suited for running processes that are expected to end after a while. For example running a lengthy batch script.
As of why it is killed, there may be a number of reasons: memory leak, server security policy .... The server probably decided your program wasn't behaving correctly. Logs of the server, like dmesg or /var/log/ contents may have some clues about it.
What you said about your app receiving visits feels like it is more a service rather than a script.
You may want to
daemonize your program
. this will make your program tied to the server availability.
Here is an explanation of the differences between nohup and daemons.
Also check this link at baeldung for help on setting up the daemon

After I check the memory usage, so I realized that my spring boot have used a lot of memory abnormally.
I solved it in the following way:
I updated my start.sh with the command of "nohup java -Xms100m -Xmx300m -jar ./target/centre-0.0.1-SNAPSHOT.jar &"
But, I did not figure out why did my spring boot application use a lot of memory?
Is there anyone can explain this phenomenon?

Related

Spring Boot Jar file start by terminal using nohup

Spring has this documentation for running an executable spring boot jar.
However, I ran this jar from terminal using the nohup linux command, and worked fine.
The question is: Using nohup or using init.d service, will have the same result for the application? Or using the init.d is the correct way always?
They do different things. nohup runs a command, and ignores the HANGUP (HUP) signal. init.d is for running a command automatically at server start-up (and shutting commands down orderly on shutdown). If you want your spring boot application to run automatically after the system restarts, put it in init.d - if you want to manually start it after every reboot you can use nohup.
nohup runs the command in a way that will be immune to hangups, which could cause problems. A lot of programs are designed to re-read their configuration files, restart, or do other things when they receive HUP signals (most services/daemons restart or re-read configs). Unless you specifically want to ignore HUP signals, using nohup isn't the best solution.
You can use & after the command in order to run it in the background, and if you want to avoid output to the terminal, you can send the output to /dev/null:
mycommand > /dev/null 2>&1 &
The 2>&1 will send stderr to stdout, so it goes to /dev/null.

Bash script to start java if not running, with other java programs running

i usually use this bash script to check if my java application is running and start it again if not. I use crontab to check it
#!/bin/bash
if [ "$(pidof java)" ]
then
# process was found
echo "application running"
else
# process not found
cd /home/assist/emanager
setsid java -jar emanager-1.0.0.jar </dev/zero &>/dev/null &
fi
Now the problem is that there are other java applications running on the server, so the script does not start my app because the if condition is true. Is there a way to check if a specific java application is running?
Thanks
Your question is very similar to this one:
Start a Java application using Bat file if not already started
The main difference is you are asking for bash while the other one is in Windows. Hence replace jcmd with jps, the rest is still applicable.

Ansible playbook to find particular java process and kill

I'm working on deploying microservices with ansible-playbook right now. And all of the microservices uses java -jar command to deploy. Right now I'm trying to write an ansible playbook to find and kill dependent java -jar process before deploying other one.
I'm running out of ideas here. I was thinking of creating a script in init.d for java deamon. But, if i do that and stop service, it would stop all the java processes which i wouldn't want.
Output for ps -ef | grep java
root 28330 1 1 13:52 ? 00:00:56 java -jar -DCONFIG_FOLDER=/opt/app/microservices/deploy/dal-core/config /opt/app/microservices/deploy/dal-core/enrollment-vehicle-dal-core-0.0.1-SNAPSHOT.jar
root 29143 1 2 14:22 ? 00:00:49 java -jar -DCONFIG_FOLDER=/opt/app/microservices/deploy/dal-core/config /opt/app/microservices/deploy/dal-core/enrollment-vehicle-listener-0.0.1-SNAPSHOT.jar
root 29879 1 2 14:23 ? 00:00:48 java -jar -DCONFIG_FOLDER=/opt/app/microservices/deploy/dal-core/config /opt/app/microservices/deploy/dal-core/enrollment-account-dal-core-0.0.1-SNAPSHOT.jar
root 31093 1 3 14:28 ? 00:01:04 java -jar -DCONFIG_FOLDER=/opt/app/microservices/deploy/listener/config /opt/app/microservices/deploy/listener/enrollment-account-listener-0.0.1-SNAPSHOT.jar
asadmin 31208 18879 0 14:57 pts/1 00:00:00 grep --color=auto java
In the above scenario, If i happen do deploy enrollment-account-dal-core again, I should 1st kill enrollment-account-listener (pid:31093) and then enrollment-account-dal-core (pid:29879).
I have one playbook for all of the microservices so I won't be able to hard code it either.
I'm not sure, but I hope that pattern parameter in service module will solve your problem. You can find documentation here. I think your Ansible task will look like this code:
- name: Killing enrollment-account-listener
service:
name: enrollment-account-listener
state: stopped
pattern: enrollment-account-listener
Documentation says, if service with given name doesn't response to service status command, then find pattern in output of ps command. If the string is found, the service will be assumed to be running.
If it won't work, you can still use command or shell module to find solution to this problem.

Background process in linux

I have developed a Java socket server connection which is working fine.
When started from a terminal, it starts from listening from client. But when I close the terminal it stops listening.
I need to continue even though the terminal closed by user from where jar file was started.
How can I run Java server socket application in Linux as background process?
There are several ways you can achieve such a thing:
nohup java -server myApplication.jar > /log.txt - this is pretty straight forward. It will just put the application in the background. This will work but it's just not a very good way to do so.
Use a shell wrapper and the above OR daemon app. This approach is used by many open source projects and it's quite good for most of the scenarios. Additionally it can be included in init.d and required run level with regular start, stop and status commands. I can provide an example if needed.
Build your own daemon server using either Java Service Wrapper or Apache Jakarta Commons Daemon. Again - both are extremely popular, well tested and reliable. And available for both Linux and Windows! The one from Apache Commons is used by Tomcat server! Additionally there is Akuma.
Personally I would go with solution 2 or 3 if you need to use this server in the future and/or distribute it to clients, end users, etc. nohup is good if you need to run something and have no time to develop more complex solution for the problem.
Ad 2:
The best scripts, used by many projects, can be found here.
For Debian/Ubuntu one can use a very simple script based on start-stop-daemon. If in doubt there is /etc/init.d/skeleton one can modify.
#!/bin/sh
DESC="Description"
NAME=YOUR_NAME
PIDFILE=/var/run/$NAME.pid
RUN_AS=USER_TO_RUN
COMMAND=/usr/bin/java -- -jar YOUR_JAR
d_start() {
start-stop-daemon --start --quiet --background --make-pidfile --pidfile $PIDFILE --chuid $RUN_AS --exec $COMMAND
}
d_stop() {
start-stop-daemon --stop --quiet --pidfile $PIDFILE
if [ -e $PIDFILE ]
then rm $PIDFILE
fi
}
case $1 in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 1
d_start
echo "."
;;
*)
echo "usage: $NAME {start|stop|restart}"
exit 1
;;
esac
exit 0
There's one crucial thing you need to do after adding a & at the end of the command. The process is still linked to the terminal. You need to run disown after running the java command.
java -jar yourApp.jar > log.txt &
disown
Now, you can close the terminal.
The key phrase you need here is "daemonizing a process". Ever wondered why system server processes often end in 'd' on Linux / Unix? The 'd' stands for "daemon", for historical reasons.
So, the process of detaching and becoming a true server process is called "daemonization".
It's completely general, and not limited to just Java processes.
There are several tasks that you need to do in order to become a truly independent daemon process. They're listed on the Wikipedia page.
The two main things you need to worry about are:
Detach from parent process
Detach from the tty that created the process
If you google the phrase "daemonizing a process", you'll find a bunch of ways to accomplish this, and some more detail as to why it's necessary.
Most people would just use a little shell script to start up the java process, and then finish the java command with an '&' to start up in background mode. Then, when the startup script process exits, the java process is still running and will be detached from the now-dead script process.
try,
java -jar yourApp.jar &
& will start new process thread,I have not tested this, but if still it not works then twite it in script file and start i with &
Did you try putting & at the end of the command line?
For example:
java -jar mySocketApp.jar &
You can also use bg and fg commands to send a process to background and foreground. You can pause the running process by CTRL+Z.
Check it out this article: http://lowfatlinux.com/linux-processes.html
Step 1.
To create new screen
screen -RD screenname
Step 2.
To enter into screen terminal
press Enter
Step 3.
Run your command or script (to run in the background) in the newly opened terminal
Step 4.
To come out of screen terminal
ctrl + A + D
Step 5.
To list screen terminals
screen -ls
that will print something like below
There is a screen on:
994.screenname (12/10/2018 09:24:31 AM) (Detached)
1 Socket in /run/screen/S-contact.
Step 6.
To login to the background process
screen -rd 994.screenname
for quite terminal and this process still working background. for me, the simple and fast way to run the process in the background is using the &! at end of the command:
if this app is built for X server: (eg: Firefox,Zathura,Gimp...)
$ java -jar yourApp.jar &!
if this app is cli (work on the terminal)
# st is my terminal like kitty alacritty
$ st -e bash -c "lookatme --style one-dark --one $1" &!

How to write init script

Hi I am using 64bit Fedora 10 linux. I have created a sample java application. Now I want to write init script for that application. so that my application should start on bootup.
How to write init script to start on bootup.
Thanks
Sunil Kumar Sahoo
There's quite a good guide here:
http://www.novell.com/coolsolutions/feature/15380.html
I'd suggest taking a look at the tomcat startup.sh and shutdown.sh scripts, and then modifying the following init.d script:
#!/bin/bash
#
# tomcat
#
# chkconfig:
# description: Start up the Tomcat servlet engine.
# Source function library.
. /etc/init.d/functions
RETVAL=$?
CATALINA_HOME="/usr/apps/apache/tomcat/jakarta-tomcat-4.0.4"
case "$1" in
start)
if [ -f $CATALINA_HOME/bin/startup.sh ];
then
echo $"Starting Tomcat"
/bin/su tomcat $CATALINA_HOME/bin/startup.sh
fi
;;
stop)
if [ -f $CATALINA_HOME/bin/shutdown.sh ];
then
echo $"Stopping Tomcat"
/bin/su tomcat $CATALINA_HOME/bin/shutdown.sh
fi
;;
*)
echo $"Usage: $0 {start|stop}"
exit 1
;;
esac
The above script is missing much of the stuff to make it fully Linux Standard Base compliant. You may want to copy an existing init.d script from your distro. A slightly better script can be found here: http://blog.valotas.com/2011/05/tomcat-initd-script.html
I usually just take one of the smaller init scripts from /etc/init.d and modify it.
Edit
The easiest thing to do is just add your program to the /etc/rc.local file. It will be the last start script executed. You won't have to mess around with the "start" and "stop" stuff.
However, if you're interested in being able to start and stop your program at will, you'll need to write a script.
Some of the other answers here will get you started.
Many distributions come with a skeleton script you can use as a template for your own init script, in /etc/init.d/skeleton or /etc/init.d/skel.
Some of the best java applications that I have seen tend to use the tanuki wrapper for this.
It standardises startup scripts across different OS's, i.e. can be used to configure a *nix daemon or a windows service.
It provides a standard command line interface for stopping, starting, restarting and checking the status - running or not.
I appreciate seeing it used more and more, as I don't have to learn it again, learn it once and reuse it again and again.
By using this service library, your application can benefit from future enhancements.

Categories