您好我的Windows 10 PC上运行了一个apache webserver,其中包含xampp和RaspberryPi,并通过脚本连接到它。该脚本检查Web服务器是否在特定端口上可用,如果可用,则启动浏览器并在服务器不再可用时关闭它。这是我的剧本:
#!/bin/bash
### BEGIN INIT INFO
# Provides: browerstartup.sh
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# X-Start-Before:
# X-Stop-After:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Interactive:
# Short-Description: If connection available, connecT!
# Description: If connection is available on port 80: Then connect.
### END INIT INFO
server="192.168.16.74"
sleep=10
while [ true ] ; do
echo "Script start" >> /home/pi/browserlog
midori=$(pgrep "midori")
nc -w 5 -z $server 80 >/dev/null 2>&1
if [ $? -ne 0 ] ; then
echo "Server isnt running!" >> /home/pi/browserlog
if [ $midori ] ; then
echo "Midori is still running but server is offline: Killing midori"
kill $midori
fi
else
echo "Server is running!" >> /home/pi/browserlog
if ! [ $midori ] ; then
echo "Starting midori" >> /home/pi/browserlog
midori -e Fullscreen -a http://$server/Website/t1&
fi
fi
echo "Sleep $sleep" >> /home/pi/browserlog
sleep $sleep
done
我第一次启动网络服务器时连接并启动midori,如果我停止网络服务器,它会关闭midori。但是,如果我再次启动网络服务器,它就不会再启动midori了,我不知道为什么......
这是我的browserlog
文件:
Script start
Server is running!
Starting midori
Sleep 10
Script start
Server is running!
Starting midori
Sleep 10
Script start
Server is running!
Starting midori
Sleep 10
Script start
Server is running!
Starting midori
Sleep 10
Script start
Server is running!
Starting midori
Sleep 10
Script start
Server is running!
Starting midori
Sleep 10
Script start
Server is running!
Starting midori
Sleep 10
Script start
Server is running!
Starting midori
Sleep 10
Script start
Server is running!
Starting midori
Sleep 10
Script start
Server is running!
Starting midori
Sleep 10
Script start
Server is running!
Starting midori
Sleep 10
Script start
我在这里手动停止了脚本!
也许你可以帮助我......
答案 0 :(得分:3)
我安装了midori并为你兄弟做了一个新剧本:)
#!/bin/bash
### BEGIN INIT INFO
# Provides: browerstartup.sh
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# X-Start-Before:
# X-Stop-After:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Interactive:
# Short-Description: If connection available, connecT!
# Description: If connection is available on port 80: Then connect.
### END INIT INFO
ssl_activated=0
server="192.168.16.74"
sleep=10
log_path="/home/pi/browserlog"
browser="midori"
if [ "${ssl_activated}" -eq 1 ]; then
parameters="-a https://${server}/Website/t1&"
else
parameters="-a http://${server}/Website/t1&"
fi
browser_pid=""
#Optional, initialize log
rm -rf "${log_path}" >/dev/null 2>&1
echo "Script start" > "${log_path}"
while true; do
echo "Loop start, checking server..." >> "${log_path}"
nc -w 5 -z ${server} 80 >/dev/null 2>&1
if [ $? -ne 0 ] ; then
echo "Server isnt running!" >> "${log_path}"
if [ -n "${browser_pid}" ]; then
echo "There is a browser, let's kill it!" >> "${log_path}"
kill "${browser_pid}" &> /dev/null
fi
else
echo "Server is running!" >> "${log_path}"
browser_pid=$(pgrep midori)
if [ -n "${browser_pid}" ]; then
echo "Midori is running and server is online" >> "${log_path}"
else
echo "Server is online but midori is not running, starting it!" >> "${log_path}"
eval "${browser} ${parameters} >/dev/null 2>&1 &"
browser_pid=$!
fi
fi
echo "Sleep ${sleep}" >> "${log_path}"
sleep ${sleep}
done