Question

I am running a process in Windows. The process is started by a batch file "skrmedpostctl_start.bat". I am writing a java project which uses the output of the skrmedpostctl. I have the system working on Linux platforms, but not on Windows. The problem is that before I execute this skrmedpostctl (a shell script in Linux, a batch file in Windows), I am checking whether it's already running or not. This is what I have so far:

switch (CURRENT_OS) {
    case LINUX:
        String[] procCheck_SKR = new String[]{"/bin/bash", "-c",  "ps -ef | grep MedPost-SKR"};
        String[] procCheck_WSD = new String[]{"/bin/bash", "-c",  "ps -ef | grep WSD_Server"};
        try {
            String procs_SKR = systemCall(procCheck_SKR);
            String procs_WSD = systemCall(procCheck_WSD);
            if (procs_SKR.split("\n").length < 2)
                systemCall(new String[]{"/bin/bash", "-c", MM_BIN_DIR + "./skrmedpostctl start"});
            if (procs_WSD.split("\n").length < 2)
                systemCall(new String[]{"/bin/bash", "-c", MM_BIN_DIR + "./wsdserverctl  start"});
        } catch (IOException e) { e.printStackTrace(); System.exit(0); }
        break;
    case WINDOWS:
        String[] procCheck = new String[]{"cmd.exe", "/c", System.getenv("windir") +"\\system32\\tasklist.exe"};
        String pidInfo = "";
        try {
            Process proc = Runtime.getRuntime().exec(procCheck);
            BufferedReader reader = new BufferedReader(new InputStreamReader( proc.getInputStream() ) );
            for (String line; (line = reader.readLine()) != null; )
                pidInfo += line;
            reader.close();
        } catch (IOException e) { e.printStackTrace(); System.exit(0); }
        if (pidInfo.contains("NAME OF MY PROCESS")) {
            // do stuff
        }
        break;
    default: throw new IllegalArgumentException("Can't run this program on " + CURRENT_OS);
}

The problem is that when I run the batch file, the task manager only shows "Windows Command Processor", and not the actual name. So, while in linux I can do a "ps -ef | grep ..." to check if it's already running, I can't seem to do the same in windows. Is there a way to do this? Any help will be greatly appreciated.

Was it helpful?

Solution

tasklist /v

In verbose mode (/v), tasklist returns the title of the window. You can test it for the name of the batch file or a special title set from inside the batch file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top