1 5 package com.tc.test.server.util; 6 7 import com.tc.util.runtime.Os; 8 9 import java.io.File ; 10 import java.io.FileOutputStream ; 11 import java.io.IOException ; 12 13 public class VmStat { 14 15 private static Process process; 16 private static File scriptFile; 17 private static final String scriptName = "capture-vmstat.sh"; 18 private static final String scriptContents = "#!/bin/sh\nexec vmstat 1 > vmstat.txt\n"; 19 20 public static synchronized void start(File workingDir) throws IOException { 21 if (process != null) { 22 stop(); 23 throw new IllegalStateException ("VmStat is already running. Stopping VmStat..."); 24 } 25 if (Os.isWindows() || Os.isMac()) return; 27 28 String [] commandLine = new String [2]; 29 commandLine[0] = "/bin/bash"; 30 commandLine[1] = createScriptFile(workingDir).toString(); 31 32 Runtime runtime = Runtime.getRuntime(); 33 process = runtime.exec(commandLine, null, workingDir); 34 35 String msg = "\n"; 36 msg += "*****************************\n"; 37 msg += "* Running vmstat in [" + workingDir + "]\n"; 38 msg += "*****************************\n"; 39 System.out.println(msg); 40 } 41 42 public static synchronized void stop() { 43 if (process != null) { 44 process.destroy(); 45 process = null; 46 deleteScriptFile(); 47 } 48 } 49 50 private static synchronized File createScriptFile(File baseDir) throws IOException { 51 if (scriptFile != null) return scriptFile; 52 File script = new File (baseDir + File.separator + scriptName); 53 script.createNewFile(); 54 FileOutputStream out = new FileOutputStream (script); 55 out.write(scriptContents.getBytes()); 56 out.flush(); 57 out.close(); 58 return scriptFile = script; 59 } 60 61 private static synchronized void deleteScriptFile() { 62 if (scriptFile != null) { 63 scriptFile.delete(); 64 scriptFile = null; 65 } 66 } 67 } 68 | Popular Tags |