简体   繁体   English

从Windows批处理脚本中的文件中读取值

[英]Reading a value from a file in a windows batch script

I'm trying to read a value from a file and use it in a subsequent command. 我正在尝试从文件中读取值并在后续命令中使用它。

I have a file called AppServer.pid which contains the process id of my app server (just the number, it's not a properties file or anything like that). 我有一个名为AppServer.pid的文件,其中包含我的应用服务器的进程ID(只是数字,它不是属性文件或类似的东西)。

The app server is hanging, so I want to take this value and pass it to the kill command. 应用服务器挂起,所以我想获取此值并将其传递给kill命令。 So my script will be something like 所以我的脚本会是这样的

SET VALUE_FROM_FILE=AppServer.pid # or something
taskkill /pid %VALUE_FROM_FILE% /f

Is there a convenient way to do this in Windows scripting? 有没有一种方便的方法在Windows脚本中执行此操作?

This works: 这有效:

SET /P VALUE_FROM_FILE= < AppServer.pid
taskkill /pid %VALUE_FROM_FILE% /f

The /P parameter used with SET allows you to set the value of a parameter using input from the user (or in this case, input from a file) 与SET一起使用的/ P参数允许您使用用户的输入设置参数的值(或者在这种情况下,从文件输入)

for /f %%G in (appid.txt) do (SET PID=%%G)
echo %PID%
taskkill etc here... 

This might help ! 这可能有所帮助!

If you know the name of the process, as returned from the command tasklist , then you can run taskkill with a filter on the process name, ie /FI IMAGENAME eq %process_name% . 如果您知道从命令tasklist返回的进程名称,则可以使用进程名称上的过滤器运行taskkill ,即/FI IMAGENAME eq %process_name%

For example, to kill all of the processes named nginx.exe run: 例如,要杀死名为nginx.exe所有进程运行:

    taskkill /F /FI "IMAGENAME eq nginx.exe"

Which "reads in English": kill all tasks (forcefully if needed via /F ) that match the filter /FI "IMAGENAME equals nginx.exe". 哪个“英文读取”:杀死所有与过滤器/FI “IMAGENAME等于nginx.exe”相匹配的任务(如果需要通过/F强制执行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM