Reputation: 12484
I have a simple Java client-server application (code linked on previous Stack question here), that I can run all together with the following Powershell code:
Start-Process java "-cp .;.\lib\commons-io-2.4.jar FixedMessageSequenceServer"
Start-Sleep -Seconds 1
$JavaClient = Start-Process java "-cp .;.\lib\commons-io-2.4.jar FixedMessageSequenceClient -Wait -PassThru"
This works fine. However, I want to add either Powershell code or Java code that would restart the above Powershell code if we force shutdown ( by CTRL-C
) the application (from the Server-side).
I thought that I should be apple to use this code :
if($JavaClient.ExitCode)
{
# exit code is non-zero, better retry
Start-Process java "-cp .;.\lib\commons-io-2.4.jar FixedMessageSequenceServer" -Wait
Start-Process java "-cp .;.\lib\commons-io-2.4.jar FixedMessageSequenceClient -Wait -PassThru"
}
However this does not do anything when I run it. Would I need to append some Java ProcessBuilder
code to achieve desired functionality ? Do we need to do processing of the $JavaClient.ExitCode
?
thanks
Upvotes: 0
Views: 362
Reputation: 12484
The following code I have works, but it's not very graceful way of doing it:
This is my Java class :
import java.io.*;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FixedMessageRunner{
public static void main(String[] args) {
// File file = new File("C://Java_Scratch_//Autonomic_Using_Batch//someFile.txt" );
try {
/* StackOverflow code */
for ( ; ; ) {
ProcessBuilder pb = new ProcessBuilder("Powershell ", "WindowsTaskSchedulerPS.PS1");
pb.directory(new File("C://Java_Scratch3_new_cleaned_Mar12//"));
Process p = pb.start();
p.waitFor();
}
/* end - StackOverflow code */
}
catch (IOException i) {
i.printStackTrace();
}
catch (InterruptedException i) {
i.printStackTrace();
}
// end-count_func
}
}
And here is my powershell code, the standard one :
# exit code is non-zero, better retry
Start-Process java "-cp .;.\lib\commons-io-2.4.jar FixedMessageSequenceServer"
Start-Sleep -Seconds 1
Start-Process java "-cp .;.\lib\commons-io-2.4.jar FixedMessageSequenceClient"
When I run the Java code, I need to kill Powershell.exe from the Windows Task Manager, in order to trigger that above waitFor()
Upvotes: 0
Reputation: 336
If you're only evaluating on a single if statement you have a small window for when it can restart even if that code you have did work.
I suggest you create a small polling script that will check the status of a certain process every 10 seconds, 1 minute, etc. and if it isn't running it can start it that way. You could nest this in an infinite loop if the java client should be running at all times, or you could use scheduled tasks to kick off the script if it should be running at a consistent interval/trigger (i.e. top of every hour, etc.)
Try something like this:
if(![bool](get-process java -ErrorAction SilentlyContinue)){
start-process java "-cp .;.\lib\commons-io-2.4.jar FixedMessageSequenceClient -Wait -PassThru"
}
Upvotes: 1