1.java程序启动时,把pid信息写出到文件:
1 public static long getPID() { 2 String processName = 3 java.lang.management.ManagementFactory.getRuntimeMXBean().getName(); 4 return Long.parseLong(processName.split("@")[0]); 5 }
2.cmd命令行脚本,读取pid,关闭程序
1 echo off 2 3 if exist "%cd%\pid" goto read_pid 4 goto start_run 5 6 :read_pid 7 for /f %%i in (pid) do ( 8 set pid=%%i 9 ) 10 11 if defined pid (taskkill /f /t /pid %pid%) else (echo server is cloased) 12 13 echo on 14 15 :start_run 16 call run.bat