【发布时间】: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 命令。我会从一个更简单的调用开始,确认它有效,然后构建更复杂的推荐链。