概要
SORACOMのSIMを使って3G回線に接続するガジェットを作ったのですが、無通信状態が1時間程度続くとセッションが切れてしまいます。
※参考ページ
無通信の状態が続くと通信が切れるのはなぜですか?
そこで、定期的にpingを飛ばす処理pythonで書いて、systemdで自動起動するようにしました。
60秒周期でsoracomにpingを飛ばす処理
この処理を、/usr/local/bin/PingSend.pyに保存します。
#!/usr/bin/python
# coding: UTF-8
import time
import os
while True:
time.sleep(60)
os.system('ping -c 3 pong.soracom.io')
pingを飛ばす処理を自動起動するユニットの作成
この処理を、/etc/systemd/system/Pi-ping.serviceに保存します。
[Unit]
Description=Ping Service
[Service]
Type=simple
ExecStart=/usr/bin/python /usr/local/bin/PingSend.py
Restart=always
User=root
[Install]
WantedBy=multi-user.target
ユニットの起動方法と自動起動の有効化
$ sudo systemctl start Pi-ping.service ★ユニットの起動
$
$ sudo systemctl status Pi-ping.service ★ユニットの状態を確認
● Pi-ping.service - Ping Service
Loaded: loaded (/etc/systemd/system/Pi-ping.service; disabled; vendor preset: enabled)
Active: active (running) since Tue 2020-07-07 21:33:27 JST; 6s ago
Main PID: 2862 (python)
Tasks: 1 (limit: 2077)
Memory: 1.7M
CGroup: /system.slice/Pi-ping.service
mq2862 /usr/bin/python /usr/local/bin/PingSend.py
7月 07 21:33:27 raspberrypi systemd[1]: Started Ping Service.
$
$ sudo systemctl enable Pi-ping.service ★ユニットの自動起動設定
Created symlink /etc/systemd/system/multi-user.target.wants/Pi-ping.service → /etc/systemd/system/Pi-ping.service.
$