首页 > 解决方案 > How to find whether vbscript is running or not

问题描述

import java.io.IOException;

public class runvbs {

    public static void RunVBs(String jobName) throws InterruptedException, IOException {
        // TODO Auto-generated method stub

        try {
            String currentDir = System.getProperty("user.dir");         
            //Execute the vbscript name which is passed in this method.
            //Generally it is runmfjob.vbs is the vbscript to run as a pre-requisite.
            Runtime.getRuntime().exec("wscript " + currentDir + "\\vbscript\\" + jobName + ".vbs");
            boolean statusofjob;            
            //Every 60 seconds monitor the jobs and print the job is completed or not.
            //Do loop executes until the job is completed.
            do {                
                System.out.println("Mainframe Job is running and waiting to complete it");
                Thread.sleep(60000);
            } while (DataGenerator.getstatusofJob() == true);
            System.out.println("Vbscript is completed");
        } catch (InterruptedException e) {
            System.out.println(e);
            System.exit(0);
        }
    }
}

public static boolean getstatusofJob() {
    boolean runflag = false;
    try {
        String line;
        String pidInfo = "";
        Process p = Runtime.getRuntime().exec(System.getenv("windir") + "\\system32\\" + "tasklist.exe");
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        // System.out.println(input);
        while ((line = input.readLine()) != null) {
            pidInfo += line;
        }
        input.close();
        if (pidInfo.contains("wscript.exe")) {
            // System.out.println("Firefox is running");
            runflag = true;
        } else {
            // System.out.println("Firefox is NOT running");
            runflag = false;
        }
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    return runflag;
}

This sometimes returns correct but not all the time. Sometimes my vbscript is completed, but it is still monitoring. Can any one help to get this to work everytime?

标签: java

解决方案


推荐阅读