I know it's quite basic question but still I am not clear about it. When I am running my jar file in background in windows using below command using bat file:
start javaw -jar myApp.jar
It start the application in background but is there any way to check if it's running or how would i kill it if I want to.
In Linux We can do ps -ef| grep JarName and then we can call kill command.How to do the same in windows. tasklist command shows java.exe instead of jar name.
Regards,
You could use jps to view current Java processes, then pass the output to taskkill. Call this script with the full package.Classname of the process to kill:
rem Run as killpid.cmd package.Classname
#echo off
jps -l |findstr %1 > %TEMP%\pid.txt
echo FOUND:
type %TEMP%\pid.txt
for /f %%i in (%TEMP%\pid.txt) do taskkill /pid %%i
Related
I am using ubuntu 14.04
I am running a jar file which should be collection a large amount of data for a few days.
I am running the jar file thought this command and it works fine.
java -jar xxx.jar
However when i close the putty, the process stopped. Is there a way for a jar file to run even when i close the putty?
You can use nohup to run the jar(any process) in background.
Use the following command in the putty session :
nohup java -jar xxx.jar &
You need the nohup command. This command makes processes keep running despite closing terminal.
Run your jar with (in case you are in the right folder):
nohup java -jar xxx.jar &
I would suggest to you use
nohup java -jar xxx.jar > /dev/null 2>&1 &
which redirects standard error & output of the command to /dev/null which means it's discarded. If you need the console output of this command then you can redirect it to any file as follows
nohup java -jar xxx.jar > output.log 2>&1 &
For the close everything crowd: This is not a duplicate of the question How to check if a process is running via a batch script because there may be multiple java.exe processes running (as they are in my case) and I need only to check for the specific jar. That's why I have written jar java application in the title.
I have a "Minecraft server" icon on my desktop that starts Spigot minecraft server. I use such run.bat file to do the job:
:start
java -Xmx1024M -Xms1024M -jar spigot-1.8.7.jar
pause
goto start
It's infinite loop to make server restarting simpler. Now it sometimes happens that I, or somebody else who's using the computer, try to start the server while it's already running. I would like to prevent this from happening. Something along:
IF spigot_server_running (
echo The server is already running. If you can't connect, try to check the console window for errors.
echo if all else fails, terminate the java process from task manager.
pause
exit 1
)
What can I do to detect it? I guess list of running processes can be parsed similarly to what Linux's grep does. But isn't there some command line tool for this distributed along with java JRE or JDK? I wouldn't install extra program for this task but I would prefer official Java API. But remember it must work with batch or Windows CScript files.
So I found out that there's this jps utility which conveniently lists running java processes and their arguments. I meantioned in the comments I think there's such a thing, but it took a while to dig out the proper use. I created following script:
#echo off
setlocal enabledelayedexpansion
echo Checking for %1
for /f "tokens=1" %%i in ('jps -m ^| find "%1"') do (
echo Jar already running
goto end
)
echo Jar not running
:end
pause
Usage:
> isjarrunning.bat something.jar
The name is case-sensitive!
In order to use this script in IF statements, it must be tweaked:
isjarrunning.bat
#echo off
IF [%1] == [] (
echo Invalid parameter. Usage:
echo isjarrunning.bat NameOfTheJarFile.jar
exit /B 2
)
for /f "tokens=1" %%i in ('jps -m ^| find "%1"') do (
exit /B 1
)
exit /B 0
Usage in your scripts:
testjarrunning.bat
#echo off
call isjarrunning.bat MyProgram.jar
echo Error level: %errorlevel%
if errorlevel 1 (
echo Jar file already running!
exit /b 1
) ELSE (
echo Starting jar file
start javaw -jar path\MyProgram.jar
)
echo Ende
I have a java app on my (Ubuntu) server. If I do this, then it starts correctly:
/usr/bin/java -cp /home/jenkins/veta/lily.jar com.sugarapp.lily.Main
but I don't know how to get its PID. I don't know how to stop it with an init.d script.
I have a different app, written in Clojure, and for it I was able to write an init.d script that works great. So I tried to refashion that init.d script for my Java app, and this is what I got:
WORK_DIR="/home/jenkins/veta"
NAME="lily"
JAR="lily.jar"
USER="jenkins"
DAEMON="/usr/bin/java"
DAEMON_ARGS=" -cp /home/jenkins/veta/lily.jar com.sugarapp.lily.Main"
start () {
echo "Starting lily..."
if [ ! -f $WORK_DIR/lily.pid ]; then
/sbin/start-stop-daemon --start --verbose --background --chdir $WORK_DIR --exec $DAEMON --pidfile $WORK_DIR/lily.pid --chuid "$USER" --make-pidfile -- $DAEMON_ARGS
else
echo "lily is already running..."
fi
}
stop () {
echo "Stopping lily..."
/sbin/start-stop-daemon --stop --exec $DAEMON --pidfile $WORK_DIR/lily.pid
rm $WORK_DIR/lily.pid
}
But this doesn't work. Although the PID in $WORK_DIR/lily.pid changes every time I run the script, no process with that PID ever seems to run. If I try:
ps aux | grep java
I don't see this app, nor if I try using the PID.
So is there a way I can use the first command, but somehow capture the PID, so I can store it for later?
I just want a reliable way to stop and start this app. That can be by PID or some other factor. I'm open to suggestions.
UPDATE:
Maybe my question is unclear? Something like jps or ps will give me too many answers. If I do something like "ps aux | grep java" I'll see that there are 5 different java apps running on the server. The start-stop-daemon won't know which PID belongs to this particular app, nor can I figure out what I should feed into my init.d script.
If your system has jdk installed there is an utility called jps which resides in jdk/bin. It will display the list of running java process. Make use of it.
If jdk is not installed in your machine then you have to grep the java process from ps -eaf command.
If you want the pid from the command line, this might work:
myCommand & echo $!
Which I copied from the accepted response to a very similar topic in ServerFault: https://serverfault.com/a/205504
I'm trying to set up a Minecraft Server and actually everything works fine. I'm starting the Server with a launch.bat file.
"%ProgramFiles%\Java\jre1.8.0_20\bin\java.exe" -Xmx2G -Xms1G -jar Tekkit.jar nogui
pause
And I have a second .bat file for backup purposes.
SET COUNTER=0
:loop
SET /a COUNTER=%COUNTER%+1
XCOPY "Server\*" "c:\Users\Benjamin\Desktop\Tekkit Server\backups\server_backup_%COUNTER%" /i /s
PING 1.1.1.1 -n 1 -w 600000 > NUL
goto loop
Now, all I'm trying to do is to start the two .bat files simultaneously, using only one .bat file.
I tried it with a third .bat file to start all:
start cmd /k CALL "C:\Users\Benjamin\Desktop\Tekkit Server\Server\launch.bat"
start cmd /k CALL "C:\Users\Benjamin\Desktop\Tekkit Server\backup.bat"
pause
It will actually start both .bat files, but suddenly, they start acting up and won't find their own files again:
C:\Users\Benjamin\Desktop>"C:\Program Files\Java\jre1.8.0_20\bin\java.exe" -Xmx2G -Xms1G -jar Tekkit.jar nogui
Error: Unable to access jarfile Tekkit.jar
C:\Users\Benjamin\Desktop>pause
Drücken Sie eine beliebige Taste . . .
C:\Users\Benjamin\Desktop>SET COUNTER=0
C:\Users\Benjamin\Desktop>SET /a COUNTER=0+1
C:\Users\Benjamin\Desktop>XCOPY "Server\*" "c:\Users\Benjamin\Desktop\Tekkit
Server\backups\server_backup_1" /i /s
File * not found
0 File(s) copied
C:\Users\Benjamin\Desktop>PING 1.1.1.1 -n 1 -w 600000 1>NUL
All three .bat files are in different directories.
Does anybody have a clue what I could do about my problem (besides stop being lazy, trying to reduce 2 clicks down to one ;) )
Thanks a lot
Benny
The problem is most likely your path directory. when you do cmd, cd to the "right" directory first, then run the bat file. for example:
cmd /k "cd /my/app/path && app.bat"
the && is the separator for multiple commands
I'm trying to execute the following Command in Java in order to kill the spawned process of bash script which is executed through java :
kill $(pgrep -P $(pgrep -P 5537))
I'm using apache Commons Exec Commandline to build the Command but it's no different to using ProcessBuilder here. So here is what I have so far:
CommandLine cmdLine = new CommandLine("bash");
cmdLine.addArgument("-c");
cmdLine.addArgument("kill $(pgrep -P $(pgrep -P "+pid+"))");
I get the error
bash: $'kill 7940\n7941\n7942\n7943': Command not found.
Normally I would now try to get the newlines out of the Command but it also doesn't work to kill just a single process because then I get the error :
bash: kill 7980: Command not found.
One the one hand I need to use bash to use the variables and on the other hand I can't use it because kill can't be executed with it...
firstly kill -9 pidnumber
Why would you need the bash variables? when java gives you strings to store variables?
Thirdly why not try System.Runtime.getRuntime().exec() ?
Also do you have permissions to kill the task? tried sudo kill -9 pid?