调度一个R脚本,在Windows上出现错误时显示弹出/消息框

问题描述 投票:0回答:1

我的目标是每5分钟检查一次网站的http状态,并在不是200的情况下发出警告信息。为了简单起见,我想根据下面给出的代码讨论我的问题。

library(httr)

a <- status_code(GET("http://httpbin.org/status/404"))

if (a == 404) system('CMD /C "ECHO Client error: (404) Not Found && PAUSE"', 
                 invisible=FALSE, wait=FALSE)

最后一点以system开头

https://heuristically.wordpress.com/2013/04/19/popup-notification-from-r-on-windows/

Show a popup/message box from a Windows batch file

上面的行结果 enter image description here

这是一个来自C:\ windows \ SYSTEM32 \ CMD.exe弹出的消息框,上面写着:

客户端错误:(404)未找到

按任意键继续...

是否有可能在此消息中添加Sys.time()?

使用taskscheduleR我安排了上面的脚本。要获得帮助,请参阅:

http://bnosac.be/index.php/blog/50-taskscheduler-r-package-to-schedule-r-scripts-with-the-windows-task-manager-2

library(taskscheduleR)

myscript <- "the address of your r script"

taskscheduler_create(taskname = "myfancyscript_5min", rscript = myscript, 
                 schedule = "MINUTE", starttime = "11:20", modifier = 5)

在这种情况下,我得到的消息框如下所示。请注意,这次它没有消息。

当我使用任务调度程序运行脚本时,如何获取写入的消息?

enter image description here

r windows command-line messagebox taskscheduler
1个回答
1
投票

您只需编辑代码的第一部分即可。正如评论中所建议的那样,我们将使用通知程序:

https://github.com/gaborcsardi/notifier

如果您在安装通知程序时遇到问题,我只能通过以下命令安装它。

devtools::install_version("notifier")

用以下内容替换第一位:

library(httr)

library(notifier)

a <- status_code(GET("http://httpbin.org/status/404"))

if (a == 404) notify(
                 title = "404",
                 msg = c("Client error: (404) Not Found")

)
© www.soinside.com 2019 - 2024. All rights reserved.