我有一个 systemd 服务文件和计时器文件,它们可以唤醒笔记本电脑并运行一个 python 脚本。然后,当这些完成后,运行一个 bash 脚本,将其重新置于挂起状态。它目前唤醒笔记本电脑,然后过一会儿挂起。不确定为什么它会给出这样的错误。当手动运行 python 脚本时,从命令行运行良好。包括计时器和服务的状态。
环境:
WXFORECAST_PUSHOVER_APP_KEY=randomAPPKey
WXFORECAST_COORDINATES=31.02,-90.56
PUSHOVER_DEVICE=weather
PUSHOVER_USER_KEY=randomGroupKey
状态服务
● wxforecast.service - Send Weather Update
Loaded: loaded (/etc/systemd/system/wxforecast.service; static; vendor preset: enabled)
Active: failed (Result: exit-code) since Fri 2020-05-29 07:00:24 CDT; 3h 56min ago
Process: 31417 ExecStopPost=/bin/bash /usr/local/bin/sus.sh (code=exited, status=0/SUCCESS)
Process: 31287 ExecStart=/usr/bin/python3 /home/william/CODE/wxforecast/wxforecast.py (code=exited, status=1/FAILURE)
Main PID: 31287 (code=exited, status=1/FAILURE)
May 29 07:00:24 william python3[31287]: File "/usr/local/lib/python3.6/dist-packages/requests/sessions.py", line 646, in send
May 29 07:00:24 william python3[31287]: r = adapter.send(request, **kwargs)
May 29 07:00:24 william python3[31287]: File "/usr/local/lib/python3.6/dist-packages/requests/adapters.py", line 516, in send
May 29 07:00:24 william python3[31287]: raise ConnectionError(e, request=request)
May 29 07:00:24 william python3[31287]: requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.weather.gov', port=443): Max retries exceeded with url: /points/35.0
May 29 07:00:24 william systemd[1]: wxforecast.service: Main process exited, code=exited, status=1/FAILURE
May 29 07:00:24 william sudo[31424]: root : TTY=unknown ; PWD=/ ; USER=root ; COMMAND=/bin/systemctl suspend
May 29 07:00:24 william sudo[31424]: pam_unix(sudo:session): session opened for user root by (uid=0)
May 29 07:00:24 william sudo[31424]: pam_unix(sudo:session): session closed for user root
May 29 07:00:24 william systemd[1]: wxforecast.service: Failed with result 'exit-code'.
状态定时器
● wxforecast.timer - Send Weather Update
Loaded: loaded (/etc/systemd/system/wxforecast.timer; enabled; vendor preset: enabled)
Active: active (waiting) since Sun 2020-05-24 18:33:02 CDT; 4 days ago
Trigger: Sat 2020-05-30 07:00:00 CDT; 20h left
sus.sh
/usr/bin/sudo /bin/systemctl suspend
wxforecast.py
## https://github.com/stacybrock/wxforecast
import os
import nwswx
import requests
(LAT, LON) = [c.strip() for c in os.getenv('WXFORECAST_COORDINATES',
'39.0693,-94.6716').split(',')]
def main():
nws = nwswx.WxAPI('[wxforecast] [email protected]')
result = nws.point_forecast(LAT, LON, return_format=nwswx.formats.JSONLD)
forecast = result['periods'][0]
# create pushover notification
title = f"{forecast['shortForecast']}"
msg = f"""{forecast['detailedForecast']}
Temp: {forecast['temperature']}°{forecast['temperatureUnit']}
Wind: {forecast['windSpeed']} {forecast['windDirection']}
Details: https://forecast.weather.gov/MapClick.php?lon={LON}&lat={LAT}
"""
r = requests.post('https://api.pushover.net/1/messages.json', data = {
'token': os.environ['WXFORECAST_PUSHOVER_APP_KEY'],
'user': os.environ['PUSHOVER_USER_KEY'],
'message': msg,
'title': title,
'device': os.environ['PUSHOVER_DEVICE']
})
if __name__ == '__main__':
main()
wxforecast.service
[Unit]
Description=Send Weather Update
Wants=network-online.target
After=network-online.target
[Service]
Type=simple
Environment="WXFORECAST_PUSHOVER_APP_KEY=RandomAppKEY"
Environment="WXFORECAST_COORDINATES=31.02,-90.56"
Environment="PUSHOVER_DEVICE=weather"
Environment="PUSHOVER_USER_KEY=randomUserGroupKey"
ExecStart=/usr/bin/python3 /home/william/CODE/wxforecast/wxforecast.py
ExecStopPost=/bin/bash /usr/local/bin/sus.sh
StandardError=journal
wxforecast.timer
[Unit]
Description=Send Weather Update
[Timer]
OnCalendar=*-*-* 07:00:00
Persistent=true
WakeSystem=true
Unit=wxforecast.service
[Install]
WantedBy=timers.target
附加信息:Xubuntu 18.04 桌面。所有相关文件都作为单独的文件包含在要点中。
Parsa Mousavi - 可能是因为 WIFI。也许我需要将 python 行移动到 shell 脚本并在运行 python 脚本之前添加 sleep 命令。我会尝试一下,明天早上再报告。这似乎是唯一的原因,因为我没有/永远不会接近任何一个月免费帐户的 7500 个 API 请求,因为这应该每天只运行一次。最近我的右腿膝盖以下截肢后,我真的很疲惫。所以显而易见的事情似乎并不像它们应该的那样明显。 :(
答案1
解决方法是 ping google 直到它响应。用下面的代码(在 Serverfault 上找到)替换了我添加的 sleep(可能可以与 vanilla ping 一起使用,但这是我选择的):
sudo apt-get install fping
HOSTNAMES=google.com
while ! fping -q $HOSTNAMES ; do :; done
现在它会 ping google 直到 google 响应,然后运行 shell 脚本中的下一个命令。
感谢大家朝着正确的方向努力。