如何使用批处理文件获取pid并将其终止?

时间:2017-03-09 12:39:50

标签: windows batch-file cmd

我必须找到特定jar文件的processid并使用相同的批处理文件将其终止。

C:\Users\k98>ps -ef|grep java
  10892   5648  0   Mar 08 con  0:02 "C:\Program Files\Java\jdk1.7.0_51\bin\java"  com.dassault_systemes.mkwebappserver.MainClass -app CATMkWebAppServerConsole\apps_list.html -autobui
  13060   7828  0 09:42:28 con 49:05 java  -Djsse.enableSNIExtension=false -Djava.util.logging.config.file=..\config\log.properties -classpath "../extlib/*";..\extlib\mysql-connector-

这里我想找到config \ log.properties文件的pid并使用批处理文件将其终止。

2 个答案:

答案 0 :(得分:0)

这就是我“找到”并杀死Adobe Acrobat的方式

::Close acrobat and any opened PDF
taskkill /im acrobat.exe /t /f 
timeout 2

/im for imagename, or program you're targeting
/t terminates the specified process and any child process which were started by it
/f Forcefully terminate the process

答案 1 :(得分:0)

通过命令行(应该包含jar)检测pid最灵活的方法是使用wmic。要处理public class MyAdapter extends ArrayAdapter<MyEntry> { public MyAdapter(Context context, int resId) { super(context, resId); } private List<MyEntry> visibleEntries() { List<MyEntry> result = new ArrayList<MyEntry>(); int i = 0; try { while (true) { if (getItem(i).isVisible()) result.add(getItem(i)); i++; } } catch(Exception e) { } return result; } @Override public int getCount() { return visibleEntries().size(); } @Override public View getView(int position, View convertView, ViewGroup parent) { LinearLayout layout = convertView == null ? (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.entry, parent, false) : (LinearLayout) convertView; MyEntry entry = visibleEntries().get(position); ... return layout; } } 结果并将其分配给您需要wmic的变量:

FOR /F

您应该使用您的jar名称更改@echo off for /f "useback tokens=* delims=" %%# in ( `wmic process where "CommandLine like '%%my_jar.jar%%' and not CommandLine like '%%wmic%%' " get ProcessId /Format:Value` ) do ( for /f "tokens=* delims=" %%a in ("%%#") do set "%%a" ) echo %processId% echo taskkill /pid %processId% /f 。要终止该过程,您必须删除最后一行中的'%%my_jar.jar%%'字。它被回应以检查是否处理了正确的进程ID。

欲了解更多信息:

WMIC

FOR /F

WQL

TASKKILL