r - 如何使用端口号从 R 中杀死 Windows 应用程序?
问题描述
我是一个愚蠢的问题。如何从 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))
解决方案
感谢@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.
推荐阅读
- mysql - 是否可以使用 Integer.parseInt 运行 sql 查询语句
- c - 哪种 USB 读取模式更有效:多次读取还是一次大读取?
- mysql - MySQL内部IP连接问题
- azure - 在 Azure 服务总线订阅上配置共享访问签名
- google-maps-android-api-2 - 如何解决chinese-mainland-android-devloper的虚拟设备无法正常显示google-map?
- c# - ExceptionHandling:如果控制器方法返回json则返回json,如果View则返回Redirect
- python - 如何在flassger(python swagger)中为auth发送通用标头
- machine-translation - Luong 注意和 Bahdanau。我们什么时候应该使用 Luong 或 Bahdanau?
- javascript - 将圆圈排列成行
- python-asyncio - 在 Aiohttp 应用程序中管理长时间运行的任务