Skip to content

Instantly share code, notes, and snippets.

@tanmally
Created October 12, 2014 11:44
Show Gist options
  • Save tanmally/2b3c34a7178e00e603b0 to your computer and use it in GitHub Desktop.
Save tanmally/2b3c34a7178e00e603b0 to your computer and use it in GitHub Desktop.
Usage : java -jar process-exec-1.0.jar 'directory' 'batch-file' 'interval'
package com.trumbleinc.exec;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ProcessExecutor {
public static void main(final String[] args) {
int length = args.length;
if (length != 3) {
System.out
.println("You need to enter directory , batch file and exec interval in mins as arguments.");
System.out
.println("Usage : java ProcessExecutor 'directory' 'batch-file' 'interval'");
System.exit(1);
} else {
System.out.println("Directory : " + args[0]);
System.out.println("Batch File : " + args[1]);
System.out.println("Interval : " + args[2] + " mins");
System.out.println("\n\n");
}
ScheduledExecutorService scheduler = Executors
.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
ProcessBuilder pb = new ProcessBuilder(new String[] {
"cmd.exe", "/c", args[1] });
pb.directory(new File(args[0]));
try {
String timeStamp = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(Calendar.getInstance().getTime());
System.out.println("Process started at " + timeStamp);
Process p = pb.start();
int exitStatus = p.waitFor();
if (exitStatus == 0) {
System.out.println(args[1] + " process ran successfully");
} else {
System.out.println(args[1] + "process Failed");
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}, 0, Integer.parseInt(args[2]), TimeUnit.MINUTES);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment