Restart tomcat through a java webapp running on it - java

I looked at this post,and they said to use a batch script to restart a tomcat server using a webapp. The issue is that if you use Runtime.exec("batch.bat"), once you do the kill for the tomcat server, the server dies and the runtime dies.
I did a quick test of this by adding some prints to my batch file to be sure:
echo "lol" > file1.txt
net stop tomcatservice
echo "lol2" > file2.txt
net start tomcatservice
echo "lol3" > file3.txt
Only file1 was created as suspected.
Is there a way to do what I want? Is there a way for me to restart the tomcat server on a schedule through code in my webapp?

Use Runtime.exec("cmd /c start batch.bat") it starts a new process so it wont be terminated when the server stops.

Create a batch file and write shutdown.bat followed by startup.bat. Call that batch file from your java code using Runtime. It should do the trick for you.

Related

Run standalone.bat from Wildfly like background process command line?

I'm having trouble installing Wildfly as a service.
So I need to be able to run the Wildlfy in standalone mode as a temporary solution, but I want it to run in the background since it is a shared server and by mistake someone can close the console:
C:\wildfly-10.1.0.Final\bin>standalone.bat --silent --close-cmd-after-start
Basically I want to be able to close the terminal once the standalone.bat is executed
The solution thanks to the suggestion of Eriksun
cd C:\wildfly-10.0.0.Final\bin>
powershell -c "saps standalone.bat -windowstyle hidden"
In case of linux you can use something like this one ./standalone.sh & . So, you can try
C:\wildfly-10.1.0.Final\bin>standalone.bat --silent --close-cmd-after-start &
which helps to run your application on background.

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.

Listen to serial port at STARTUP using java with arguments on Debian(Rasbian)

I've been stuck two weeks trying to figure out how to run this at startup.
I use the following chain of commands on the terminal:
1. source ~/.bashrc
2. source ~/.tinyos.sh
3. java net.tinyos.tools.Listen -comm serial#/dev/ttyUSB0:telosb | python demo.py`
The third command uses java to listen to the serial port and pipes it to a python script which cleans, converts and uploads to mysql localhost.
This works fine on ssh terminal. But ive tried using nohup+update-rc.d, upstart, systemd, crontab to make it run on startup and it just wont work! When I reboot and check logs / database, its as if the command never happened. I need this to run like a daemon and continue running until shutdown.
Thanks a lot.
How are you trying to execute the program ? Are there are permission issues accessing / executing the script ?
Which version of debian are you running - look at upstart scripts if you are running Jesse+
I'd put those three lines in a bash script and use upstart scripts to trigger them on start. Another option is to use supervisord to make sure that your scripts run and restart if for any reason the program crashes.

Wait till tomcat startup is over

I am executing startup.bat programatically to start a tomcat server.
ProcessBuilder processUnzip = new ProcessBuilder("cmd", "/c", "start", "C:\\apache-tomcat-6.0.37\\startup.bat");
I need to wait till the complete startup.bat process is over. But the program ends immediately after the startup.bat is started.
Is there a way to wait till startup process is over
Update:
My requirement is to deploy a WAR file in a tomcat instance. I cannot use hot deployment or dynamic deployment. I have to do a static deployment. Basically the task is to automate the manual process of build deployment in tomcat.
You are facing this problem because startup.bat opens up a new window with Tomcat console and returns immediately.
Even if you use catalina.bat instead of startup.bat, that would still not work as that process would terminate only when Tomcat stops.
Technically, You would never know whether the tomcat is started on or until the logs tell you so. So the approach you are taking might not work.
The only crude solution I can think of is (with the same code you have), once you start the Tomcat, keep checking the console log file, at intervals, for specific string (like Server started etc) which indicate that the server has started.
BTW, if you could tell us your specific use case (why are you doing this ?), community here might come up with better alternatives.
To automate the process, you should copy the app to be deployed into tomcat deployments folder before you start the server up. If you copy any .war package to TOMCAT/webapps folder, it will get deployed by Tomcat when it starts up.
So what you need to do is to just copy the file. here are four different examples on how to do that in Java. Either that, or you can just exec a copy command.
Possibly easiest and least error prone is to do it using Files, in java 7:
Files.copy(source.toPath(), dest.toPath());
and after that is finished, then exec start command.
Update: with shutdown, you have the wait issue. You can wait doing something like this in a batch file:
CALL shutdown.bat
:LOOP
tasklist /FI "WINDOWTITLE eq Tomcat" | find /C /I ".exe" > NUL
IF ERRORLEVEL 1 (
GOTO :EOF
) ELSE (
ECHO Tomcat is still running
SLEEP 1
GOTO LOOP
)
This code assumes a Tomcat window with name 'Tomcat'. If you run Tomcat as a service, for example, that assumption is not true. For service however there are more reliable means using sc query:
SC stop "tomcat"
:LOOP
SC query "tomcat" | FIND "STATE" | FIND "RUNNING" > NUL
IF ERRORLEVEL 1 (
GOTO :EOF
) ELSE (
ECHO Tomcat is still running
SLEEP 1
GOTO LOOP
)
Here assuming service name "tomcat".
Update2: forgot we're talking about java here - with the bat above, you can use java .waitFor(), or you can program the same logic in a java file as well.
You can use Runtime to create a process and then wait for the process to finish.
You do that by doing the following.
Process process = Runtime.getRuntime().exec("cmd /c start file.bat");
int exit_value = process.waitFor();
Where in your case the file.bat is the file you want to execute.
Remove the start command to run the batch file in the foreground - then, waitFor() will wait for the batch file completion:
Process p = Runtime.getRuntime().exec("cmd /c " + path +
"\RunFromCode.bat");
If RunFromCode.bat executes the EXIT command, the command window is automatically closed. Otherwise, the command window remains open until you explicitly exit it with EXIT - the java process is waiting until the window is closed in either case.

Running jar and database through batch file (Windows)

I have made shortcuts within my file directory to run the startNetworkServer and my jar file. These are two serparate shortcuts that I then run within a single batch file. My batch file starts the network server, then pings 1.1.1.1 with a wait of 5 seconds before then running the jar file, hence opening my java GUI.
My issue is, I wish to stop the network server again upon closing my java GUI program. To do this I know I can run the stopNetworkServer command, however, placing this in the batch file after the starting of my jar file results in the network server stopping before or whilst the jar file is running. I want it to stop upon exiting my java program.
Is there anyway I can check to see if the jar file is open? Or tell it to wait until the jar file is closed? Or even return something in my java code to kick start this?
J
Fixed, just had to add a /W prefix before running my Jar file link. This means that the next process will not start until that one finishes.
The final batch file (.bat) looks like this:
START startNetworkServer.lnk
PING 1.1.1.1 -n 1 -w 5000 >NUL
START /W Run.exe.lnk
START stopNetworkServer.lnk
J

Categories