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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...