Debian 11 にあります。 FilePwOn.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が表示されます)。