开机联网自动发送消息
有时需要在板子连接网络时知道它的ip地址,这里介绍一种无需安装任何第三方软件的方式实现开机联网时自动将本机ip地址通过bark服务发送到自己的手机上.
- 确认本设备支持systemd方式自启动
if [[ `/sbin/init --version` =~ upstart ]];
then echo Upstart;
elif [[ `systemctl` =~ -\.mount ]];
then echo Systemd;
elif [[ -f /etc/init.d/cron && ! -h /etc/init.d/cron ]];
then echo SysV-Init;
else echo Unknown;
fi
如果打印了’systemmd’则可以继续往下看
2. 添加发送消息服务
vi /etc/systemd/system/bark.service
添加:
[Unit]
Description=Bark Service
After=network.target
[Service]
Type=idle
User=nobody
Restart=on-failure
RestartSec=10s
ExecStart=/bin/bash /home/abc/send_ip_to_me.sh
[Install]
WantedBy=multi-user.target
其中send_ip_to_me.sh
内容如下:
name=$(uname -n)
ip=$(hostname -I)
msg="$name $ip"
echo sending msg:$msg
curl https://api.day.app/***/"$msg" # 需要注册一个bark的key
- 启动服务
sudo systemctl start bark && sudo systemctl enable bark
如果出现错误,可以使用命令systemctl status bark
和journalctl -u <service_name>
查看错误细节.