1

Hello I have an apache webserver running on my Windows 10 PC with xampp and a RaspberryPi to connect to it with a script. The script checks if the webserver is available on specific port and if ist available start the browser and close it if the server is not available anymore. Here's my Script:

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

The first time I start the webserver it connects and starts midori and if I stop the webserver it closes midori. But if I start the webserver again it doesnt start midori any more and I have no idea why...

Here is my browserlog file:

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

Here I stopped the script manually!

Maybe you can help me...

WasteD
  • 758
  • 4
  • 24

1 Answers1

3

I installed midori and did a new script for you brother :)

#!/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
OscarAkaElvis
  • 5,384
  • 4
  • 27
  • 51
  • I have some questions first of all I'm just testing your code ty :D So what does `>/dev/null 2>&1` and why do you always or mostly use `"${var}"`? – WasteD Dec 06 '16 at 11:39
  • I put >/dev/null 2>&1 because starting midori shows on screen "trash" at least in my case... it shows `openjdk version "1.8.0_111" OpenJDK Runtime Environment (build 1.8.0_111-8u111-b14-3-b14) OpenJDK 64-Bit Server VM (build 25.111-b14, mixed mode)` and I use "${var}" because I use [shellcheck](https://github.com/koalaman/shellcheck) always to audit my bash code to avoid warnings – OscarAkaElvis Dec 06 '16 at 11:44
  • 1 more question are the `@keyframe` tags from css3 working in your midori? It doesn't work for me :/ – WasteD Dec 06 '16 at 11:47
  • Ohh I never used midori before... I tried only to do the script with my home router page... and everything seems fine... javascript is working... css too, but I think my router's webpage hasn't css keyframes... maybe if you tell me some page to test available on internet... Ahh, I edited the script to change one line to avoid one shellcheck warning, is the line which "catchs" the browser pid. Now I use pgrep which is better :) – OscarAkaElvis Dec 06 '16 at 11:51
  • I tested midori with this page using keyframes and is working fine: https://css-tricks.com/snippets/css/shake-css-keyframe-animation/ – OscarAkaElvis Dec 06 '16 at 12:02
  • Does your midori start again if you stop the webserver and start it again? – WasteD Dec 06 '16 at 12:18
  • Yes, I tested it against my home's router webpage... If I unplug (for example) the network cable... the script works well and kill the browser... and after plugin it again, then it starts again the browser with no problems! – OscarAkaElvis Dec 06 '16 at 12:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129908/discussion-between-wasted-and-oscarakaelvis). – WasteD Dec 06 '16 at 12:28