【问题标题】:How to kill an windows application from R using a port number?如何使用端口号从 R 中杀死 Windows 应用程序?
【发布时间】:2019-01-14 13:48:38
【问题描述】:

这是一个愚蠢的问题。如何从 R 传递以下 windows 命令(杀死在 1234 端口上运行的进程):

for /f "tokens=5" %a in ('netstat -aon ^| find ":1234" ^| find "LISTENING"')
    do taskkill /f /pid %a

到目前为止,我已经尝试过......

# Create the string
kill <- "for /f \"tokens=5\" %a in ('netstat -aon ^| find \":1234\" ^| find \"LISTENING\"') do taskkill /f /pid %a"

# Check
cat(shQuote(kill, type="cmd"))
# "for /f \"tokens=5\" %a in ('netstat -aon ^| find \":1234\" ^| find \"LISTENING\"') do taskkill /f /pid %a"

# Run the cmd
system(shQuote(kill, type="cmd"), wait = F)

# Warning message:
#   In system(shQuote(kill, type = "cmd"), wait = F) :
#   '"for /f \"tokens=5\" 0x0p+0 in ('netstat -aon ^| find \":1234\" ^| find \"LISTENING\"') do taskkill /f /pid 0x0p+0"' not found

编辑:我的帮助

我得到了一个引号组合,它给出了(cat)与 win 命令相同的字符串。

kill <- 'for /f "tokens=5" %a in (\'netstat -aon ^| find ":1234" ^| find "LISTENING"\') do taskkill /f /pid %a'
cat(kill)
# From Cat:    for /f "tokens=5" %a in ('netstat -aon ^| find ":1234" ^| find "LISTENING"') do taskkill /f /pid %a
# Win Command: for /f "tokens=5" %a in ('netstat -aon ^| find ":1234" ^| find "LISTENING"') do taskkill /f /pid %a

额外:

以下代码将在端口 1234 上运行一个闪亮的应用程序。试图从另一个 R 会话中终止该应用程序。

library(shiny)

ui <- fluidPage(

)

server <- function(input, output, session) {

}

shinyApp(ui, server, options = list(launch.browser = TRUE, port = 1234))

【问题讨论】:

  • 这可能并不重要,但是当您使用shQuote 时,您在支票中使用type="cmd",而不是实际调用。也许你可以尝试让更简单的系统调用工作,然后从那里开始构建。
  • @TimBiegeleisen 是的.. 我错过了。两者都会给出相同的结果,可以通过相同(shQuote(kill,type =“cmd”),shQuote(kill))检查。虽然我会编辑。
  • 我的猜测是您需要使用shell(),因为for 不是可执行文件而是shell 命令。我会从一个更简单的调用开始,确认它有效,然后构建更复杂的推荐链。

标签: r cmd


【解决方案1】:

感谢@HenrikB 的建议。是的...以下带有 shell() 函数的代码正在运行...

# Create the string
kill <- 'for /f "tokens=5" %a in (\'netstat -aon ^| find ":1234" ^| find "LISTENING"\') do taskkill /f /pid %a'

# Check
cat(kill)
# From Cat:    for /f "tokens=5" %a in ('netstat -aon ^| find ":1234" ^| find "LISTENING"') do taskkill /f /pid %a
# Win Command: for /f "tokens=5" %a in ('netstat -aon ^| find ":1234" ^| find "LISTENING"') do taskkill /f /pid %a

# Run the cmd
shell(kill)
# E:\Raja\Installed_Software\R-3.5.1>taskkill /f /pid 18772 
# SUCCESS: The process with PID 18772 has been terminated.

【讨论】:

    猜你喜欢
    • 2019-08-14
    • 1970-01-01
    • 2015-05-25
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    相关资源
    最近更新 更多