微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

R 管道工 api 在控制台中工作,但不适用于 systemd

如何解决R 管道工 api 在控制台中工作,但不适用于 systemd

我有一个定期发送数据的气象站,我想与管道工一起制作一个 API 来接收和保存 JSON 数据。我的脚本是

# plumber.R
library(tidyverse)
library(lubridate)

#' @post /station
#' @serializer json
#' @param wind_direction_raw
#' @param rain_amount_raw
#' @param timestamp
#' @param elapsed_time
#' @param wind_speed_raw
#' @param message_id
function(req,wind_direction_raw,rain_amount_raw,timestamp,elapsed_time,wind_speed_raw,message_id){

    new_data <- tibble("wind_direction_raw" = wind_direction_raw,"rain_amount_raw" = rain_amount_raw,"wind_speed_raw" = wind_speed_raw,"timestamp" = ymd_hms(paste(timestamp[1:6],collapse="="),tz = "UTC"),"elapsed_time" = elapsed_time,"message_id" = message_id)

    write_path <- paste0("/home/pi/weather_station/data/weather_data_",Sys.Date(),".csv")

    readr::write_csv(new_data,write_path,append=TRUE)

当我打开 R 会话并输入

> library(plumber)
> weather_service <- pr('/home/pi/weather_station/weather_api/receive_json.R')
> pr_run(weather_service,port=9494,host='192.168.1.151')

然后在另一台计算机上用 curl with

发送一些 json
curl -X POST -H "Content-Type: application/json" --data '{"elapsed_time": 6245,"timestamp": "[2020,7,26,12,2,21,6,208]","wind_direction_raw": 108,"wind_speed_raw": 5,"message_id": 666,"rain_amount_raw": "0"}' http://192.168.1.151:9494/station

它工作正常。与我的实际气象站相同。

但是当我使用这个 systemd 服务文件

[Unit]
Description=Plumber API

[Service]
ExecStart=/usr/bin/Rscript -e "library(plumber); pr_run(pr('/home/pi/weather_station/weather_api/receive_json.R'),host='192.168.1.151')"
Restart= always
RestartSec=60
SyslogIdentifier=WeatherAPI

[Install]
WantedBy=multi-user.target

气象站或我的 curl 示例都不能发送数据。它似乎在发送进程中间挂起,因为 curl 进程挂起并且永远不会完成。

sudo systemctl status 看起来不错

Feb 03 17:24:25 gatesPi4 systemd[1]: Started Plumber API.
Feb 03 17:24:29 gatesPi4 WeatherAPI[14465]: ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
Feb 03 17:24:29 gatesPi4 WeatherAPI[14465]: ✔ ggplot2 3.3.2     ✔ purrr   0.3.4
Feb 03 17:24:29 gatesPi4 WeatherAPI[14465]: ✔ tibble  3.0.1     ✔ dplyr   1.0.0
Feb 03 17:24:29 gatesPi4 WeatherAPI[14465]: ✔ tidyr   1.1.0     ✔ stringr 1.4.0
Feb 03 17:24:29 gatesPi4 WeatherAPI[14465]: ✔ readr   1.3.1     ✔ forcats 0.5.0
Feb 03 17:24:30 gatesPi4 WeatherAPI[14465]: ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
Feb 03 17:24:30 gatesPi4 WeatherAPI[14465]: ✖ tidyr::extract()   masks magrittr::extract()
Feb 03 17:24:30 gatesPi4 WeatherAPI[14465]: ✖ dplyr::filter()    masks stats::filter()
Feb 03 17:24:30 gatesPi4 WeatherAPI[14465]: ✖ dplyr::lag()       masks stats::lag()
Feb 03 17:24:30 gatesPi4 WeatherAPI[14465]: ✖ purrr::set_names() masks magrittr::set_names()
Feb 03 17:24:30 gatesPi4 WeatherAPI[14465]: Attaching package: ‘lubridate’
Feb 03 17:24:30 gatesPi4 WeatherAPI[14465]: The following objects are masked from ‘package:base’:
Feb 03 17:24:30 gatesPi4 WeatherAPI[14465]:     date,intersect,setdiff,union
Feb 03 17:24:30 gatesPi4 WeatherAPI[14465]: Running plumber API at http://192.168.1.151:9494
Feb 03 17:24:30 gatesPi4 WeatherAPI[14465]: Running swagger Docs at http://192.168.1.151:9494/__docs__/

有什么想法吗?我尝试将 Users=pi 添加到服务文件中,以用户身份而不是 root 身份运行,但行为没有变化。有什么想法吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。