R Plumber气象站数据接收API在控制台正常运行但systemd部署后失效的问题求助
I've run into similar headaches deploying Plumber APIs via systemd before—let's walk through targeted troubleshooting steps and fixes that should resolve your hanging requests:
1. Verify Port Listening & Network Binding
First, confirm the API is actually accessible on the network. On your Pi, run this command to check if the port is being listened to:
ss -tulpn | grep 9494
- If you get no output, the API isn't binding to the port successfully. This could happen if your Pi's IP changed (making
host='192.168.1.151'invalid) or systemd's network context restricts interface access. Try switching the host to0.0.0.0in yourpr_run()call to allow connections from any interface. - If you see the port listening only on
127.0.0.1, that explains why external requests hang—your API is only accessible locally. Using0.0.0.0will fix this.
2. Add Debug Logs to Catch Silent Errors
Your current systemd setup doesn't capture detailed R error output, which means failures might be happening silently. Modify your service file to log all output to a file for debugging:
[Unit] Description=Plumber API After=network.target # Ensure network is fully ready before starting the service [Service] User=pi WorkingDirectory=/home/pi/weather_station/weather_api/ # Set working directory to your script's location ExecStart=/usr/bin/Rscript -e "options(error=traceback); library(plumber); pr_run(pr('receive_json.R'), port=9494, host='0.0.0.0')" >> /home/pi/weather_station/plumber_debug.log 2>&1 Restart=on-failure # Restart only on actual failures (not endlessly) RestartSec=10 SyslogIdentifier=WeatherAPI [Install] WantedBy=multi-user.target
- The
options(error=traceback)will log a full stack trace if any R code crashes. - Check
/home/pi/weather_station/plumber_debug.logafter restarting the service—this will reveal issues like missing libraries, failed timestamp parsing, or write permission errors.
3. Fix File & Directory Permissions
Even with User=pi, systemd might run with a restricted environment that blocks writes to your data directory. Ensure the pi user has full access:
sudo chown -R pi:pi /home/pi/weather_station/data/ sudo chmod -R u+rwx /home/pi/weather_station/data/
This guarantees the API can create and append to CSV files without permission issues.
4. Align R Environment with Your Interactive Session
Systemd often runs R with a different library path than your interactive terminal. In an R session, run .libPaths() to get your user-specific library path, then add it to the ExecStart command:
ExecStart=/usr/bin/Rscript -e ".libPaths('/home/pi/R/x86_64-pi-linux-gnu-library/4.0'); options(error=traceback); library(plumber); pr_run(pr('receive_json.R'), port=9494, host='0.0.0.0')" >> /home/pi/weather_station/plumber_debug.log 2>&1
Replace the path with the output from .libPaths() on your system—this ensures R loads the same tidyverse/lubridate versions you use interactively.
5. Isolate the Issue with a Minimal API
To narrow down whether the problem is in your data processing code or the API deployment, temporarily replace your Plumber script with a simplified version:
# receive_json.R library(plumber) #' @post /station #' @serializer json function(req){ list( status = "success", received_data = req$body, server_time = Sys.time() ) }
Restart the service and test with curl again. If this works, the issue is in your original code (e.g., the ymd_hms() call failing on the timestamp format). If it still hangs, the problem lies with network binding or systemd's environment.
Final Reminder
After editing the systemd service file, don't forget to reload the daemon and restart the service:
sudo systemctl daemon-reload sudo systemctl restart your-service-name
内容的提问来源于stack exchange,提问作者pgcudahy

