Trying to start two .bat files with a .bat file - java

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

Related

How to find the process id that is running my Java Application

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

Use batch to only start a jar java application if it's not already running

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

Current directory is invalid when running batch file

This is my run.bat file:
#echo off
start compactau.exe
"c:\program files\java\jre7\bin\java" -Xms512m -Xmx768m -cp jio.jar;log4j-1.2.9.jar;auagent.jar Auagent auagent.conf
Pause
When I run this file, I get the error the current directory is invalid. I uninstalled and reinstalled Java, then changed my Java location to c:\java\bin\java and adjusted my batch file like this:
#echo off
start compactau.exe
"c:\java\bin\java" -Xms512m -Xmx768m -cp jio.jar;log4j-1.2.9.jar;auagent.jar Auagent auagent.conf
Pause
But I still get the the current directory is invalid. How is this possible when I created the destination and folder name?
#echo off
start /D DBServerSimulator ./DBServerSimulator.exe
start /D DetectCheatServer ./DetectCheatServer/DetectCheatServerR
start /D ServerGuarder ./ServerGuarder/ServerGuarder.exe
start /D ManagerServer ManagerServer/ManagerServerR
start /D GateServer ./GateServer/Gate
start /D LocalSaveServer LocalSaveServer/LocalSaveServerR
start /D ShareMemProc ./ShareMemProc/ShareMemProcR
start /D GameServer ./GameServer/Line.exe
Start /D Auagent run.bat!
is my start.bat the last thing to start is my run.bat it starts the whole server boots up but because java wont kick in i get errors which i cant post coz it's image file only and i cant post them due to being a new user lol
you didn't specify a title to start. Use quotes for an empty title
start "" compactau.exe
Looks it is missing the directory where compactau.exe is stored.
Can you just add a line as
cd <path where the compactau.exe is stored>
e.g. cd c:\personal\exe_dir
before "start compactau.exe" so it can use the directory?
However the question is not clear enough in which line you get the error. Can you please mention it or post the error log?
and what are you going to do with Auagent auagent.conf, are they parameters to main method() ?
why don't you try by setting the path first before you run your java command?
You need not provide the whole absoulute path for using java command.

using rc.local to start a jar file at startup is not working

I have a .jar file I want to run whenever the system reboots/starts, so I put the line
nohup java -jar /mnt/fioa/fusion/nfs/labStats/LabInfoAutoLog.jar > /dev/null &
in my /etc/rc.local file. The program is validated as working, and if I run the above command at the command line the program works as expected.
Other versions I have tried without success:
nohup /usr/bin/java -jar /mnt/fioa/fusion/nfs/labStats/LabInfoAutoLog.jar > /dev/null &
and:
nohup java -jar /mnt/fioa/fusion/nfs/labStats/LabInfoAutoLog.jar 2> /dev/null \ .. &
I am running centos 6.4.
Check that your jar file is accesible roots, NFS mounted volumes may impose special restrictions for root.
Instead of discarding your error messages, you might want to route them to syslog, something like 2> /sbin/logger -t FOO 1> /sbin/logger -t BAR
Maybe the path isn't set yet at startup time and you need the full path to the java executable or, possibly, nohup.

shell scripting: java program can't find property file when starting by sh

I am starting a java programm under Red Hat Enterprise Linux Server release 5 (Tikanga).
directory structure:
- bin ->sc.jar,start-sc.sh,sc-lib-all.jar
- conf->log4j-sc.properties,sc.properties
command to run the java programm (which is perfectly working):
/usr/java/jdk1.6.0_37/bin/java -Xmx1024M -Dlog4j.configuration=file:../conf/log4j sc.properties -jar sc.jar -config ../conf/sc.properties
if i put it into a shell script the java programm can't find the prop file anymore.
shell script (start-sc.sh) looks like:
#!/bin/sh
/usr/java/jdk1.6.0_37/bin/java -Xmx1024M -Dlog4j.configuration=file:../conf/log4j-sc.properties -jar sc.jar -config ../conf/sc.properties
i am a newbie on shell scripting any ideas what i am missing? thx!
i guess you started your shell script not from the bin directory, which the dir start-sc.sh belongs to.
to explain it clear, let's make an example.
say, your script is here:
/foo/bar/bin/start-sc.sh
if you start it under /foo/bar/bin/, it (the relative path) should work.
but if you start your script from /home/yourHome/someDir/ , the relative path will point to $PWD/../, which is /home/yourHome/
you could either in your script first cd /foo/bar/bin/ before you start the java app. or do something like:
a=`dirname $0`
if [ $a = '.' ];then
a=`pwd`
fi
cd $a
/usr/java/jdkxxxx/java .....
It sound fine to me, does this version work?
#!/bin/sh
/usr/java/jdk1.6.0_37/bin/java -Xmx1024M -Dlog4j.configuration=file:$(pwd)/../conf/log4j-sc.properties -jar sc.jar -config $(pwd)/../conf/sc.properties
Edit #1:
Try put the following before launching your program:
echo `pwd`
The output tells you where you are running your script, so you can check if it's the right path or not.
Edit #2:
Try this script
#!/bin/bash
LOG4JCONF="/absolute/path/to/the/log4j/conf/file"
SCCONF="/absolute/path/to/the/other/conf/file"
/usr/java/jdk1.6.0_37/bin/java -Xmx1024M -Dlog4j.configuration=file:$LOG4JCONF -jar sc.jar -config $SCCONF

Categories

Resources