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

时间:2021-06-02 23:31:33

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. So my script will be something like

应用服务器挂起,所以我想获取此值并将其传递给kill命令。所以我的脚本会是这样的

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脚本中执行此操作?

3 个解决方案

#1


14  

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参数允许您使用来自用户的输入来设置参数的值(或者在这种情况下,从文件输入)

#2


2  

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

This might help !

这可能有帮助!

#3


1  

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

如果您知道从命令任务列表返回的进程名称,则可以使用进程名称上的过滤器运行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强制执行)。

#1


14  

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参数允许您使用来自用户的输入来设置参数的值(或者在这种情况下,从文件输入)

#2


2  

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

This might help !

这可能有帮助!

#3


1  

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

如果您知道从命令任务列表返回的进程名称,则可以使用进程名称上的过滤器运行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强制执行)。