Я создал простой скрипт, который через равные промежутки времени выгружает содержимое .txt на канал Slack.
Когда я запускаю его в RStudio, он работает нормально, но когда я запускаю его из командной строки через командный файл, он не работает. Код выполняется, печатает ожидаемые результаты и не падает, но в выбранном канале Slack ничего не появляется. Что происходит?
logger.r
#required packages:
require(httr)
require(tidyverse)
library(jsonlite)
options(stringsAsFactors = F)
#System directories:
winLogRoot = "C:/Users/Dave/source/repos/window logger/Release"
current = paste0(winLogRoot,"/example.txt")
#httr stuff:
url = "https://hooks.slack.com/services/[webhook url here]"
waitTime = 60 # how often to check the text file
while(1)
{
#Get first row of table
winName = read.table(file = current, sep = "\n")
sessHist = tibble(ts = as.character(Sys.time()),winName = paste0(winName$V1,"\n"))
for(waits in 1:(300/waitTime)) # Send to slack every five minutes
{
print(waits) #debug string
Sys.sleep(waitTime)
winName = read.table(file = current, sep = "\n")
## Add another row to the tibble
sessHist = rbind(sessHist,tibble(ts = as.character(Sys.time()), winName = paste0(winName$V1,"\n")))
}
#Turn the tibble into a string and post it to Slack
print("attemping upload") #debug string
body = toString(sessHist%>%as.matrix %>% t)
body = paste0('{"text":"',body,'"}')
print(body) #debug string
#This is the bit that only seems to work when I run in RStudio
httr::POST(url = url,body=body,encode = 'json')
}
logger.bat :
"C:\Program Files\R\R-3.5.3\bin\i386\Rscript.exe" logger.R
Выход командной строки :
C:\Users\Dave\Documents\logger\win logger Rpart>"C:\Program Files\R\R-3.5.3\bin\i386\Rscript.exe" logger.R
Loading required package: httr
Loading required package: tidyverse
-- Attaching packages --------------------------------------- tidyverse 1.2.1 --
v ggplot2 3.1.0 v purrr 0.3.1
v tibble 2.0.1 v dplyr 0.8.0.1
v tidyr 0.8.3 v stringr 1.4.0
v readr 1.3.1 v forcats 0.4.0
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
Attaching package: 'jsonlite'
The following object is masked from 'package:purrr':
flatten
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] "attemping upload"
[1] "{\"text\":\"2019-05-12 15:26:46, C:\\WINDOWS\\system32\\cmd.exe\n, 2019-05-12 15:32:55, C:\\WINDOWS\\system32\\cmd.exe\n, 2019-05-12 15:39:05, C:\\WINDOWS\\system32\\cmd.exe\n, 2019-05-12 15:45:16, C:\\WINDOWS\\system32\\cmd.exe\n, 2019-05-12 15:51:26, C:\\WINDOWS\\system32\\cmd.exe\n, 2019-05-12 15:57:36, C:\\WINDOWS\\system32\\cmd.exe\n\"}"
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] "attemping upload"
[1] "{\"text\":\"2019-05-12 15:57:37, C:\\WINDOWS\\system32\\cmd.exe\n, 2019-05-12 16:03:47, C:\\WINDOWS\\system32\\cmd.exe\n, 2019-05-12 16:09:57, C:\\WINDOWS\\system32\\cmd.exe\n, 2019-05-12 16:16:07, C:\\WINDOWS\\system32\\cmd.exe\n, 2019-05-12 16:22:17, C:\\WINDOWS\\system32\\cmd.exe\n, 2019-05-12 16:28:27, C:\\WINDOWS\\system32\\cmd.exe\n\"}"
[1] 1
[1] 2
Так что, очевидно, он запускает скрипт, но не успешно использует webhook.
Бонусное содержание: экзистенциальный кризис (текстовый файл содержит имя переднего плана)
[1] 5
[1] "attemping upload"
[1] "{\"text\":\"2019-05-12 19:45:38, Why does this webhook work in RStudio, but not command line? - Stack Overflow - Google Chrome\n, 2019-05-12 19:51:41, Why does this webhook work in RStudio, but not command line? - Stack Overflow - Google Chrome\n, 2019-05-12 19:57:44, Why does this webhook work in RStudio, but not command line? - Stack Overflow - Google Chrome\n, 2019-05-12 20:03:48, Why does this webhook work in RStudio, but not command line? - Stack Overflow - Google Chrome\n\"}"
[1] 1
[1] 2