Running multiple Java Applications on startup - java

I currently run these java applications with the following command via root:
java -Xms1G -Xmx1G -jar /var/www/tekkit.socialnetwk.com/tekkit.jar nogui
Although if I close the terminal window those applications close/stop.
Is there anyway to stop this from happening? Either creating it as a service or to start it on boot.
I've tried rc.local but no luck.
I'm running on Ubuntu - Newest.

In the past I have done two things to make a process run after the terminal shuts down... use ampersand to run it in the background and nohup so that it does not get killed by the terminal closing.
nohup java -Xms1G -Xmx1G -jar /var/www/tekkit.socialnetwk.com/tekkit.jar nogui &
EDIT: Here is a great answer that goes into detail. Upvote that answer instead since it is way more complete.

Related

Why is jstack unable to open socket file when trying to generate a thread dump?

I am trying to generate a thread dump of a java process being run on a Linux instance in AWS. I am using the jstack command on OpenJDK version 1.8.0. The current command I am running is sudo -u <user> jstack -l <java pid> where <user> is the user that started the JVM.
When I run this, I receive the error Unable to open socket file: target process not responding or HotSpot VM not loaded
Potential Problem:
While reading about how jstack works, I noticed that jstack is supposed to generate a socket file /tmp/.java_pidXXX in order to attach to the process. This file is not generated.
My potential solution is that if I can get the socket file to generate, hopefully jstack will be able to run properly.
I am unsure why this error is occurring, but my only idea is that this could this be some kind of permissions error to create files in the /tmp directory. I tried testing my permissions by creating text files in the /tmp directory and I was able to create text files.
How can I get this socket file to generate? Any potential solutions would be greatly appreciated.
Edit
Here I have added the command that was used to create the JVM. The command used to get this command was ps -aux | grep java
java -server -Xmx8192m -XX:+HeapDumpOnOutOfMemoryError ->XX:HeapDumpPath="/tmp" -XX:MaxPermSize=256M -Djava.awt.headless=true ->Dsling.run.modes=dynamicmedia_scene7,,
<instance_name>,samplecontent,crx3,crx3ta>r -Djava.locale.providers=CLDR,JRE,SPI -jar crx-quickstart/app/cq-?>quickstart-6.5.0-standalone-quickstart.jar start -c crx-quickstart -i >launchpad -p 4502 -Dsling.properties=conf/sling.properties
Solution:
An update for anyone who comes across this in the future. I found a solution that worked for me by changing the command that I was using to initially start the JVM. I added the flag -XX:+StartAttachListener which forces the process to generate the /tmp/.java_pidXXX socket file during the booting of the JVM.
Other tips I came across in my journey to finding this solution:
Make sure the user executing the jstack command is the same user that who ran the process that you are trying to take the thread dump of.
Also, make sure the socket file /tmp/.java_pidXXX is not being automatically cleaned up by any background cleanup processes in the /tmp directory.

Run java process as background process in linux

I am running my project as jar using java -jar command in Linux machine. As soon as this program run , It produces logs in another directory. Running my program this way requires me to keep the shell open. Now If I have to see the logs , I can't do that in the same shell. I am forced to do that by either doing the duplicate session or new session. Is there any way I can run the jar as background process and see the logs in the same shell ?
If you don't care about it staying alive, something as simple as nohup java -jar myjar.jar & should work. If you need it to be automatically restarted if it crashes or start automatically at boot, you'll want to look into something like systemd or monit.

How to open java program in other linux computer without terminal holding?

I have written a java program with jar file. The java program is to update status of linux server so it need to keep running, but the linux server is in data center, so I need to remote to server to open the program. I use ssh to login linux server. Use command of "java -jar file.jar" to run the program.
However, the java program of the linux server will close if I close the terminal in my computer. Since I cannot keep opening my computer, I wanna know how to open the java programming without holding my computer terminal.
you need to use nohup to keep the program running after you log out.:
server:~name$> nohup java -jar file.jar &
this will keep your program running
Two ways
One
nohup java -jar file.jar &
Another
java -jar file.jar &
In both cases your process will go in background however the process will terminate in the second approach when shell terminates in second case.
If this program is intended to be running on all your machines for monitoring purposes, you should be running it as a service from your server's init system (systemd for most systems these days). You can use the Java Service Wrapper or jsvc or write your own init script.
Another solution apart from the proposed one:
screen -d -m java -jar your.jar
You will then have a detached screen with your java command in it. List with screen -l, reattach with screen -D -RR <screenid_obtained_via_screen_-ls>

How to update bash script whilst running or easily restart it?

I require a method to easily restart/replace bash scripts without manually stopping/starting the process over again.
Here's how my current script works:
#!/bin/sh
while true
do
echo "1"
java -server -Xms2G -jar game.jar nogui
done
Basically, if the game stops, it will automatically restart however, the script is already loaded into memory so if I was to modify it, I would have to manually kill the script and start it up again in order for the changes to take effect. I'm running at least 200 instances of the game server itself so, it would not be intelligent to do this manually and kill, then start each script manually again.
When the game server stops, I wish for it to update the script, here is an example of what I may want to do:
#!/bin/sh
while true
do
echo "1"
echo "2"
java -server -Xms2G -jar game.jar nogui
done
However, I'd need to physically stop the script and start it again myself in order for it to add the new 'echo "2"', I need an easier way to replace the running script.
The script itself will download the updated script when the game stops (so it will automatically start again.
How can I make the script unload itself from memory, and use the new script?
If I cannot do this, is there any alternative method you can suggest?
The alternative method is to use a process supervision tool such as runit, daemontools, upstart, supervisord, systemd, etc.
For any of these, you would write only the following script:
#!/bin/sh
exec java -server -Xms2G -jar game.jar nogui
...and the supervision tool would be responsible for restarting the server on exit, rereading the script (and thus responding to updates) each time it needs to be run.
These tools give you far more features than just reliable restarts -- they typically also manage logging, staged shutdown (ie. TERM + timeout + KILL), custom cleanup commands, signal hooks, etc.
See also the ProcessManagement page on the freenode #bash wiki.
If you can't use a process management system (and you really, really should!), you can have the script exec itself. That is to say:
#!/bin/sh
# ...do stuff here...
exec "$0" "$#" # and restart
Note that this is unreliable for various reasons related to BashFAQ #28.
Call the following file myscript.sh. It reloads itself into memory every time that the game stops:
#!/bin/sh
java -server -Xms2G -jar game.jar nogui
exec /path/to/myscript.sh

Linux Launch java program on startup (EC2 instance)

my server program needs to be launched on the startup of an EC2 instance. At the minute im just launching it from my SSH with the following commands:
java -jar ~/DocumentManager/DocumentServer-0.2.jar
I tried adding this to the .bashrc and /etc/rc.local files but they only seem to work when i ssh in.
Anyone know how to make it so an instance of my application is launched when the computer boots?
Thanks,
Ben
It's possible you can create a script java_server_launch.sh like this:
#! /usr/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
JAVA=/usr/bin/java
MY_SERVER=/home/your_username/DocumentManager/DocumentServer-0.2.jar
USER=your_username
/bin/su - $USER -c "$JAVA -jar $MY_SERVER &"
Put your script under /etc/init.d directory, and then use the command:
update-rc.d java_server_launch.sh defaults
more on update-rc.d command by using man update-rc.d.
Hope this help.
Regards.
Add ampersand(symbol '&') at the end of the command.
For example, in your case, java -jar ~/DocumentManager/DocumentServer-0.2.jar &
Old question, but my answer might be helpful for people who look in future.
You can also run your program as a service which automatically run on ec2 container reboot. Below link worked for me:
https://medium.com/#lizlieholleza/run-your-java-application-as-a-service-in-an-ec2-instance-amazon-linux-d7c7b4c0b2f4

Categories

Resources