auto restart batch files - java

My apologizes if this question is asked before but i couldn't find a correct match with my situation.
I have some batch files, they are always running because of the java program running inside them. However, sometimes they crash and since i am not monitoring them, some amount of time passes till i restart them.
I want to ask that is there a way to monitor those batch files, or how can i restart those batch files even if they crash. I am running those files on Windows XP.
Thanks

Have you tried this?
http://www.ghacks.net/2009/08/31/monitor-and-restart-crashed-windows-processes/

You can start a batch file from another batch file in a loop. When it is started, the loop waits. Once it crashes - the loop starts it over.

Replace each call to java with a goto loop that calls java via cmd.exe. Conditionally loop back only if the java program exited with an error.
.
.
:javaLoop
cmd /c java yourJavaProg || goto :javaLoop
.
.
You might want to guard against an endless rapid loop in the case of a complete failure by adding a counter to limit the number of times it restarts. Another possibility is to note the time and only restart if a minimum amount of time has elapsed since the last restart. Or perhaps a combination of both.
EDIT - On second thought, you may not need the CMD /C. It may work fine just by invoking java directly and conditionally looping upon error.

Related

Is it better to launch a Java app once and sleep or repeat launching and killing?

I have a Java application that needs to run several times. Every time it runs, it checks if there's data to process and if so, it processes the data.
I'm trying to figure out what's the best approach (performance, resource consumption, etc.) to do this:
1.- Launch it once, and if there's nothing to process make it sleep (All Java).
2.- Using a bash script to launch the Java app, and when it finishes, sleep (the script) and then relaunch the java app.
I was wondering if it is best to keep the Java app alive (sleeping) or relaunching every time.
It's hard to answer your question without the specific context. On the face of it, your questions sounds like it could be a premature optimization.
Generally, I suggest you do what's easier for you to do (and to maintain), unless you have good reasons not to. Here are some possible good reasons, pick the ones appropriate to your situation:
For sleeping in Java:
The check of whether there's new data is easier in Java
Starting the Java program takes time or other resources, for example if on startup, your program needs to load a bunch of data
Starting the Java process from bash is complex for some reason - maybe it requires you to fiddle with a bunch of environment variables, files or something else.
For re-launching the Java program from bash:
The check of whether there's new data is easier in bash
Getting the Java process to sleep is complex - maybe your Java process is a complex multi-threaded beast, and stopping, and then re-starting the various threads is complicated.
You need the memory in between Java jobs - killing the Java process entirely would free all of its memory.
I would not keep it alive.
Instead of it you can use some Job which runs at defined intervals you can use jenkins or you can use Windows scheduler and configure it to run every 5 minutes (as you wish).
Run a batch file with Windows task scheduler
And from your batch file you can do following:
javac JavaFileName.java // To Compile
java JavaFileName // to execute file
See here how to execute java file from cmd :
How do I run a Java program from the command line on Windows?
I personally would determine it, by the place where the application is working.
if it would be my personal computer, I would use second option with bash script (as resources on my local machine might change a lot, due to extensive use of some other programs and it can happen that at some point I might be running out of memory for example)
if it goes to cloud (amazon, google, whatever) I know exactly what kind of processes are running there (it should not change so dynamically comparing to my local PC) and long running java with some scheduler would be fine for me

Repeated execution of a java file

I have a java file which i want to execute repeatedly on a machine. The file should be executed automatically as soon as the previous instance of the file execution is over. How to achieve this?
Use bash script in a loop call your executable
for i in 1 2 3 4 5 .. N
do
java executable
done
Java provides an environment in which a program will function as you (or someone else) have written it.
The operating system specifics will determine how that program is started. If you want the program to launch each time the previous copy has finished, you need to wrap the launching of the java program with a loop in the scripting language supported by that operating system, or convert the program to be a system service / daemon.
Other techniques exist, but they actually don't launch the second program after the first completes. They either:
Launch a second copy of the program immediately (unless already found). The program is written to do nothing while it can detect it's "launcher" is running. Upon loss of one process, the second becomes responsible for the processing (and the launching of it's standby failover).
Capture shut down / crash events and launch a copy of the program when quitting, which actually has two programs running at the same time, but for a short duration. This is commonly done; but is technically unsound as there are many ways a program might crash before being able to launch a new process.
You can write a simple perl or python script that would work on both windows and linux.
#!/usr/bin/perl
$N = 100;
for($i = 0; $i < $N; $i++){
system('/full/path/to/java -cp full_class_path my.class.Name');
}
You can call this script in a java file or execute it on command line.
Runtime.getRuntime().exec("perl script.pl")

How to kill a particular process in java

Hi all i Have two process in my java application as below.
Process p1=some process;
Process p2=some process;
At the first time it starts its working perfectly.But when i reload the application the older process is also running so i just want to kill the older process if its executing.I know that we can kill a process by using its process id.But how can i get the process id of this p1 and p2.Any idea?.
Catch the reload event in your application and terminate the child process before you exit.
If that doesn't work, you probably have a bug in your design. Try to fix that bug. Really. I mean it.
A workaround is to write the PID into a text file. When you start, read the text file, check if this is the correct process and then kill it.
Without the check, you might be killing a perfectly valid process which was created by someone else. My reasoning is like so: You can't kill the process when you reload. That means you can't delete the text file either. So the PID file will always exist. Eventually, you will kill the wrong process. That will happen once per year. It will happen the first time six months after you left/changed job/etc. Nobody will understand what is going on because it's almost impossible to reproduce.
As others have said, it would be better to terminate the process in a more natural way.
However, if you do need to actually forcibly terminate it, then you could call the destroy() method on the Process object, whenever you don't care about it running anymore.
Also, do you really need to run the code as a Process? There are generally better ways.

How to restart stuck Java program?

My program is fairly large, and because it tends to carry out processes randomly, at times it gets stuck and loops forever. If I were to forcefully stop and restart the program manually, usually (around 85%) of the time, the program completes all commands and terminates.
Is there a way to make a Java program restart itself after say 20 seconds, if it gets stuck? I tried using the system time to solve the issue, but the problem with this is that if my program gets stuck in a for loop, it does not update the system time until the next iteration.
This isn't the right way to approach this problem! You need to figure out why your program is getting stuck in an infinite loop, and then fix it. "Okay let's try this again" is not the right way to solve a bug - you have no idea what other effects this bug could be having. You might very well be getting incorrect output as well. Debug the program, don't work around the flaw.
You could use some external program that launches the java program and kills it after 20 seconds when it gets stuck, and then launches it again, but again, that is not the right way to solve the problem.
Considering that solving this problem would mean solving the Halting problem, we can be fairly sure that the whole approach is doomed ;)
You could obviously use timers to kill the program after some specified time and whatnot, but really - find the bugs in your program.
Your program shouldn't stuck and loops forever, repair this. But if this isn't possible and you still want "restart" program after forever-loop I propose you such solution:
Create main program which will be director. The director will be create thread. The thread will be doing main algorithm which can take a lot of time. The director will be waiting some time which will be final parameter. This parameter help to director recognises if the thread is in forever loop (it takes too long time). When forever-loop will be recognize, it'll terminate the thread and start new one (restart).
Have a look at ExecutorServices to get a mechanism that allows you to invoke a piece of code and receive a timeout if it doesn't finish within the expected time. You can then act upon as you see fit.
Another nice tool is to use jvisualvm in the JDK to attach to the program when looping. You can then ask for a thread dump and use it to figure out what it is doing.

Sequential execution of java programs == sequential activation of the jvm?

I've a bash script that sequentially calls a java program. That's a bit tricky but mainly what i do is to have a loop and executes about 1500 times the same java program with different arguments.
My question is, when using java 1.5 (the sun VM), each time I'm calling the java program a new instance of the jvm is created ? (I'm not sure that's the right vocabulary...)
Should I avoid this situation by introducing a level of indirection, i.e building a list of all the parameters and then executing one java program which takes these parameters and executes what was previously my entry point ?
Or can I deal with the problem by configuring the JVM as resident or something like that and dynamically invokes my program....
hope this is clear....
thx...
You could save the parameters into a file and use the Java program to process it without constant restart. You could also pipe in the parameters into the running Java app through the console, similarly as for example ls | grep java
Edit: And for the first question. I doubt the java runtime would stay deliberately in memory. Probably most JRE files would remain in the disk cache anyway. On Windows there is a Java Quick Start service which keeps the JRE files around to reduce the startup time for a java program. Don't know whether there is a similar thing for *nix.
Obviously having all the parameters beforehand and running the program once after that would be the best solution. If you cannot do that for any reason i have a very dirty solution to this. Have your program register a port and listen to it for input. Then simply pass the arguments to that port and have your program handle them as a new instance.
JVM startup is notoriously slow, and it certainly not intended to be done in a loop like that. Unfortunately the only way to avoid this if you are passing command line parameters to the java program is to modify the java program itself in some way to have alternative forms of interaction (either from the console, or a port, or a read a file). Java Quick Start is the only (closest thing to a) solution if the java program cannot be changed.
The real solution is to change the Java program. The most obvious change would be to have your loop write to a file, and then start the java program that would read the file one line at a time. That works if the loop doesn't care about the results from the java program for the next set of parameters.
If it does, then it would really be necessary to understand that relationship to advise on an appropriate solution. The socket solution suggested by Savvas is certain a general purpose solution, but there may be better options, depending on what you need to accomplish.
You can use a launcher like in the answer to
Simultaneously run java programs run on same JVM? to read input line by line and start your program's main() method.

Categories

Resources