它在 Debian 11 上。文件 PwOn.service:
[Unit]
Description=Send a message with a telegram bot
After=network.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=/usr/bin/JubiBotStartup.sh
[Install]
WantedBy=multi-user.target
bash 脚本 JubiBotStartup.sh 为
#!/bin/bash
TOKEN="randomToken"
ID="11111111"
DT=$(date +'+%d-%m-%Y')
HR=$(date +'+%H:%M:%S')
MSG="System powered on $DT , $HR"
URL="https://api.telegram.org/bot$TOKEN/sendMessage"
curl -s -X POST $URL -d chat_id=$ID -d text="$MSG"
我已经启用了该服务,因此当我输入命令 systemctl status 时,它看起来就像这样。当我使用“systemctl start PwOn.service”启动服务时,它可以正常工作。
但是,当系统开机或重新启动时,它不起作用,检查其状态显示如下:
PwOn.service - Send a message with a telegram bot
Loaded: loaded (/etc/systemd/system/PwOn.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Tue 2023-03-07 04:22:37 CET; 21min ago
Process: 474 ExecStart=/usr/bin/JubiBotStartup.sh (code=exited, status=6)
Main PID: 474 (code=exited, status=6)
CPU: 129ms
Mar 07 04:22:36 debian systemd[1]: Started Send a message with a telegram bot
Mar 07 04:22:37 debian systemd[1]: PwON.service: Main process exited, code=exited, status=6/NOTCONFIGURED
Mar 07 04:22:37 debian systemd[1]: PwOn.service: Failed with result 'exit-code'
~
答案1
如果要保证网络上线后设备正常运行,需要列出network-online.target
在两个都 Wants=
和After=
:
[Unit]
Description=Send a message with a telegram bot
After=network-online.target
Wants=network-online.target
目前,您的设备在网络启动期间运行,但curl
由于无法解析目标主机(这是其退出代码 6 所指示的内容)而失败。