Killing Java process gracefully using WMIC - java

I have to kill a Java process gracefully from windows. The requirement is that there is a Java main class XYZ.java which has a shutdown hook added named ABC.java containing some piece of code.I have to run code present in ABC.java when XYZ.java terminates.
In linux, I am able to do so using 'kill' command.
In windows, I used below command but that's not killing my class gracefully:
WMIC PROCESS where "Name='java.exe' AND CommandLine LIKE '%%XYZ%%'" CALL TERMINATE.However, I am able to achieve this windows by running CTRL+C while running java process XYZ from cmd.
Please suggest.

Related

How to find and close running Win-Process which is Java app from within other Java app?

There is windows process which is actually a java application (java.exe). I need to close it from within other java application (other running .jar) which is probably will also be named as java.exe in windows taskbar.
To do the task we would normally do it this way
Runtime.getRuntime().exec(KILL + serviceName); // where serviceName is a `java.exe`
But there would be some java.exe processes in windows taskbar. So how to find the one I need? In this case it is forum.jar.
Actually this jar was started from .bat file (if it makes any difference) - it is process.bat as it seen in the screenshot.
You could run
wmic process where caption="java.exe" get commandline,description,processid
to get the list of processes and their associated command line like in your screenshot. Then you get the ID of the command you want to kill and kill it with taskkill /pid xxx.

Spawning process in background on Jenkins - job that won't stay in queue

I want to make job on Jenkins that starts server (MockServer on WireMock).
Server is launched from *.jar file, from terminal like that.
java -jar serverLaunch.jar
It takes over my console. To avoid that I modify this and do:
java -jar serverLaunch.jar &>/dev/null &
And that works for me on my local PC. Now I want to move it to Jenkins.
If I try to do this from "Shell command" block in Jenkins Job then:
a) java -jar serverLaunch.jar
I have task locked in queue in my Jenkins and I don't want that but server starts and works.
b) java -jar serverLaunch.jar &>/dev/null &
Job ends with success but my server is not alive.
I have wrapped this command also in .sh script and .rb script. Any idea how to make it work?
I've tried this:
https://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+build
And then in Jenkins "Shell script":
daemonize -E BUILD_ID=dontKillMe /bin/bash launch.sh
But it also passes but server is not alive.
I had to check "Inject environment variables to the build process" and add:
BUILD_ID=dontKillMe
Now it is working.
Try using nohup e.g.:
nohup java -jar serverLaunch.jar &
That should prevent the process being terminated when the parent shell process exits (which I suspect is your problem).
Another effective approach would be to add a post-build action that executes a shell spawning the server.

Linux command for pseudo terminal

I have a csh script which requires TTY to work properly.
I need to execute this script from a program which does not have TTY (like Java program started from Eclipse).
I tried "script" Linux command and it works in general, but it has a side effect.
This command forks itself with PTY and my csh script runs under child (slave) process.
But when parent (master) process is killed (by Java program), child process remains running and detached to "init" process.
So, is there any way to run csh in pseudo-terminal, so that all processes are killed when killing parent process (which Java program has access to)?
I know that some Java libraries exist for this (pty4j, etc.) but I have no possibilities to use them, so I need a Linux command.

Running java jar file as a daemon and specify pid path

I'm running CentOS 6 and I have a jar file I'd like to execute with java and have it run in the background. I plan on executing this command from a script, and I also would like to specify the path at which the pid file is output. How can I do all this?
You can use Java Service(Commons Daemon) from apache. Instead of implementing main, your class to execute must provide method for the service execution (start, stop, etc.)

How to set up a cron job to start and stop a java jar?

I have compiled my JAVA code into a jar file which I have ported to my ubuntu server. I can start it manually the usual way using java -jar myJar.jar but I'd like my program to be active only for 8 hours. How can I go about setting my jar file up as a process which starts at 9AM and also which automatically closes at 5PM?
I would write a simple launcher script that does the following:
Takes two command line options:
--start
Set up the classpath and environment like JAVA_HOME for the jar to run.
Spawn java -jar myJar.jar.
Capture the process ID and store it in the myJar.pid file in a specific location.
--stop
Read the process ID from myJar.pid and send a kill signal.
Then schedule two jobs in cron, one to call this launcher script with --start argument, at 9AM, and the other to call the same script with --stop argument, at 5PM.
I would also have a shutdown hook registered in my application to gracefully exit when the kill signal is issued.

Categories