Reading a value from a file in a windows batch script

微笑、不失礼 提交于 2020-01-01 08:59:32

问题


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).

The app server is hanging, so I want to take this value and pass it to the kill command. 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?


回答1:


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)




回答2:


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

This might help !




回答3:


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, i.e. /FI IMAGENAME eq %process_name%.

For example, to kill all of the processes named nginx.exe run:

    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".



来源:https://stackoverflow.com/questions/298292/reading-a-value-from-a-file-in-a-windows-batch-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!