jrobinsonc
3/15/2017 - 4:56 PM

Run PHP and NGROK.

Run PHP and NGROK.

#!/bin/bash

LOCK_FILE=`basename $0.lock`

getPID() {
    ps aux | grep "[l]ocalhost:$PORT" | awk '{print $2}'
}

isRunning() {
    if [ "`getPID | wc -l | sed 's/ //g'`" = "2" ] && [ -f $LOCK_FILE ]
    then
        retval="true"
    else
        retval="false"
    fi

    echo "$retval"
}

start() {
    if [ "`isRunning`" = "false" ] 
    then
        php -S localhost:$PORT -t ./ 1>/dev/null &

        ngrok http localhost:$PORT 1>/dev/null &

        URL=""
        while [ "$URL" = "" ] ; do
            URL="`wget -q -O - 'http://127.0.0.1:4040/api/tunnels' | sed -E 's/^.*public_url":"(https[^"]+).+$/\1/g'`"

            if [ "$URL" = '{"tunnels":[],"uri":"/api/tunnels"}' ]
            then
                URL=""
            fi
        done

        echo "PORT=$PORT" > $LOCK_FILE

        echo 'Process started.'
        echo 
        echo 'URL:'
        echo $URL
        echo 
    else
        echo 'Process is already running.'
    fi

}

stop() {
    if [ "`isRunning`" = "false" ] 
    then
        echo 'Process was not running.'
    else
        kill `getPID`
        echo 'Process stopped.'

        if [ -f $LOCK_FILE ]
        then
            rm $LOCK_FILE
        fi
    fi
}

if [ "`isRunning`" = "true" ] 
then
    source $LOCK_FILE
else
    PORT=$2

    if [ "$PORT" = "" ]
    then
        PORT=9000
    fi
fi

case "$1" in 
    start)
       start
       ;;
    stop)
       stop
       ;;
    restart)
       stop
       start
       ;;
    status)
       if [ "`isRunning`" = "false" ] 
       then
          echo 'Stopped'
       else
          echo 'Running'
       fi
       ;;
    *)
       echo "Usage: $0 {start|stop|status|restart}"
esac

exit 0 

Run PHP and NGROK

Documentation soon.