1

您好,我有一个在我的 Windows 10 PC 上运行的 apache 网络服务器,带有 xampp 和一个 RaspberryPi,可以通过脚本连接到它。该脚本检查特定端口上的网络服务器是否可用,如果可用,则启动浏览器并在服务器不再可用时关闭它。这是我的脚本:

#!/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

在这里,我手动停止了脚本!

也许你能帮我...

4

1 回答 1

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
于 2016-12-06T11:22:51.220 回答