Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux RPi和Ubuntu服务器之间通信的最佳方式_Linux_Bash_Raspberry Pi - Fatal编程技术网

Linux RPi和Ubuntu服务器之间通信的最佳方式

Linux RPi和Ubuntu服务器之间通信的最佳方式,linux,bash,raspberry-pi,Linux,Bash,Raspberry Pi,所以我目前正在研制一种自动CO/烟雾探测器,它使用RasPi作为控制器。我希望它发送短信警报时,检测事件发生。我希望它能像这样工作 Sensor(s) > RPi > Main Server > SMS via sendmail 我想知道如何在RPi和我的服务器之间保持稳定的链接。我曾考虑使用SSH和Ping以及Supervisord来保持一切正常工作,但是我还希望能够测试脚本是否使用来自主服务器的命令远程工作 宋承宪和Ping看起来是解决这个问题的好方法吗?如果是这样,我如

所以我目前正在研制一种自动CO/烟雾探测器,它使用RasPi作为控制器。我希望它发送短信警报时,检测事件发生。我希望它能像这样工作

Sensor(s) > RPi > Main Server > SMS via sendmail
我想知道如何在RPi和我的服务器之间保持稳定的链接。我曾考虑使用SSH和Ping以及Supervisord来保持一切正常工作,但是我还希望能够测试脚本是否使用来自主服务器的命令远程工作

宋承宪和Ping看起来是解决这个问题的好方法吗?如果是这样,我如何确保RPi上的脚本没有崩溃,即使设备正在响应ping

谢谢


编辑:不需要实时通信,我想每10秒检查一次,我建议你在Raspberry Pi上使用类似的脚本

#!/usr/bin/env bash

SVIP=192.168.0.9    # Server IP address
PORT=30000

################################################################################
# This function is called whenever the script exits or dies
################################################################################
function cleanup(){
   echo DEBUG: EXIT code running...
   # Kill the alive function we started in the background
   kill -9 $alivePID
}
trap cleanup EXIT

################################################################################
# This function runs continuously in the background answering "pings"
################################################################################
function alive(){
   echo DEBUG: Listening for pings on port $PORT
   while :; do
      echo "$RANDOM" | nc -l $PORT
      echo DEBUG: Ouch.
   done
}


################################################################################
# main function of script
################################################################################

# Start the ping responder in the background and note its PID so we can kill it
alive &
alivePID=$!

# Do whatever you want to do here - e.g. read temperatures
sleep 20
从底部开始读大约4行。它在后台启动名为
alive()
的函数,并获取其PID,以便在退出时停止。
alive()
函数使用
netcat
侦听“ping”,并用随机数回复每个ping

如果脚本停止,它将终止
alive()
函数,因此它将不再回复ping

在您的服务器上,您可以通过键入以下命令“ping”Raspberry Pi(将RASPI_IP替换为Raspberry Pi的IP地址):

echo“PING”| nc 30000;回声$?

如果服务在Raspberry Pi上运行,您将得到一个随机数和退出代码
0
,然而,如果Raspberry Pi上的服务未运行,您将获得退出代码
1
,并且没有回复。

您可能会发现
netcat
nc
bash
shell中用于从一台机器向另一台机器发送短字符串。您还可以将测试字符串从服务器发送到Raspberry Pi,它可以将其反转,或添加1或其他内容,然后将其发送回,这将确保它正常工作。因此,稍微阅读一下
netcat
会使它看起来可行,它是否只返回发送的字符串?还是会有其他信息与之一起发送?
echo "PING" | nc <RASPI_IP> 30000 ; echo $?