[SOLVED] Start blynk server on Runlevel (Raspberry Pi)

Hi,

i would like to start blynk server on raspberry pi start. For that i programmed a runnlevel script. After System start i can see that blynk can’t find the directory which the server.properties is stored. Blynk creats its log directory directly under /
the blynk server.jar is under /opt/blynk. Can enyone help me. Thanks
The init Script is:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          Blynk
# Required-Start:    $syslog
# Required-Stop:     
# Default-Start:  2 3 4 5   
# Default-Stop:   0 1 6   
# Short-Description: Blynk for Raspberry
# Description:       Blynk for Raspberry
### END INIT INFO
 
# Actions
case "$1" in
    start)
		/usr/bin/java -jar /opt/blynk/server-0.12.1.jar -dataFolder /opt/blynk/Blynk &
		echo "Server gestartet"
        ;;
    stop)
        pkill -9 -f server-0.12.1.jar
		#screen -S "Blynk" -X quit
        ;;
    restart)
        $0 stop
        $0 start
        ;;
esac
 
exit 0

The blynk.log is the following:

11:19:57.261 INFO  - Application server port 8443.
11:19:57.279 INFO  - Administration server port 8777.
11:46:11.846 INFO  - Using data dir '/opt/blynk/Blynk'
11:46:14.583 INFO  - Initializing mail transport. Username : example@gmail.com. SMTP host : smtp.gmail.com:587
11:46:16.405 INFO  - Plain tcp/ip hardware server port 8442.
11:46:16.410 INFO  - Enabling SSL for hardware.
11:46:16.418 WARN  - ATTENTION. Certificate / and key / paths not valid. Using embedded certs. This is not secure. Please replace it with your own certs.
11:46:24.642 INFO  - SSL hardware port 8441.
11:46:24.646 INFO  - Enabling HTTPS for hardware.
11:46:24.646 WARN  - ATTENTION. Certificate / and key / paths not valid. Using embedded certs. This is not secure. Please replace it with your own certs.
11:46:26.142 INFO  - HTTPS hardware server port 7443.
11:46:26.177 INFO  - Enabling SSL for application.
11:46:26.178 WARN  - ATTENTION. Server certificate paths cert : '/', key : '/' - not valid. Using embedded server certs and one way ssl. This is not secure. Please replace it with your own certs.
11:46:28.133 INFO  - Application server port 8443.
11:46:28.151 INFO  - Administration server port 8777.

This one is wrong, should be:

/usr/bin/java -jar /opt/blynk/server-0.12.1.jar -dataFolder /opt/blynk

I think … or you should create the “Blynk” folder manual and set permissions.

Btw, it may be advisable to create a more elaborate startup script, something like:

#!/bin/bash
#

BASE=/tmp
PID=$BASE/blynk.pid
LOG=$BASE/blynk.log
ERROR=$BASE/blynk-error.log

COMMAND="java -jar /storage/blynk/server.jar -dataFolder /storage/blynk"

status() {
	echo 
	echo "==== Status"
	
	if [ -f $PID ]
	then
		echo
		echo "Pid file: $( cat $PID ) [$PID]"
		echo
		ps -ef | grep -v grep | grep $( cat $PID )
	else
		echo
		echo "No Pid file"
	fi
}

start() {
	if [ -f $PID ]
	then
		echo
		echo "Already started. PID: [$( cat $PID )]"
	else
		echo "==== Starting"
		touch $PID
		if nohup $COMMAND >>$LOG 2>&1 &
		then
			echo $! >$PID
			echo "Done"
			echo "$(date '+%Y-%m-%d %X'): START" >>$LOG
		else
			echo "Error"
			/bin/rm $PID
		fi
	fi
}

kill_cmd() {
	SIGNAL=""; MSG="Killing "
	while true
	do
		LIST=`ps -ef | grep -v grep | grep server.jar | awk '{print$1}'`
		if [ "$LIST" ]
		then
			echo; echo " $MSG $LIST" ; echo
			echo $LIST | xargs kill $SIGNAL
			sleep 2
			SIGNAL="-9" ; MSG="Killing $SIGNAL"
			if [ -f $PID ]
			then
				/bin/rm $PID
			fi
		else
			echo; echo "All killed" ; echo
			break
		fi
	done
}

stop() {
    echo "==== Stop"

    if [ -f $PID ]
    then
        if kill $( cat $PID )
        then echo "Done."
             echo "$(date '+%Y-%m-%d %X'): STOP" >>$LOG
        fi
        /bin/rm $PID
        kill_cmd
    else
        echo "No pid file. Already stopped?"
    fi
}

case "$1" in
    'start')
            start
            ;;
    'stop')
            stop
            ;;
    'restart')
            stop ; echo "Sleeping..."; sleep 1 ;
            start
            ;;
    'status')
            status
            ;;
    *)
            echo
            echo "Usage: $0 { start | stop | restart | status }"
            echo
            exit 1
            ;;
esac

exit 0

This also makes a PID file and such to prevent starting more than one server :slight_smile:

Hi,

thanks for the awsome script. But i have still problems to start Blynk after restart of raspberry.
If i start the script manualy /etc/init.d/blynk start then everthing is working fine. If i restart the raspberry, blynk didn’t find the folder /opt/blynk which includes all the config files. Why? Thanks!

i get the following messages in /logs/blynk.log (the folder /opt/blynk/Blynk is correct).

09:47:07.435 INFO  - Using data dir '/opt/blynk/Blynk'
09:47:10.421 INFO  - Initializing mail transport. Username : example@gmail.com. SMTP host : smtp.gmail.com:587
09:47:12.245 INFO  - Plain tcp/ip hardware server port 8442.
09:47:12.250 INFO  - Enabling SSL for hardware.
09:47:12.258 WARN  - ATTENTION. Certificate / and key / paths not valid. Using embedded certs. This is not secure. Please replace it with your own certs.
09:47:15.238 INFO  - SSL hardware port 8441.
09:47:15.242 INFO  - Enabling HTTPS for hardware.
09:47:15.244 WARN  - ATTENTION. Certificate / and key / paths not valid. Using embedded certs. This is not secure. Please replace it with your own certs.
09:47:19.434 INFO  - HTTPS hardware server port 7443.
09:47:19.469 INFO  - Enabling SSL for application.
09:47:19.470 WARN  - ATTENTION. Server certificate paths cert : '/', key : '/' - not valid. Using embedded server certs and one way ssl. This is not secure. Please replace it with your own certs.
09:47:21.639 INFO  - Application server port 8443.
09:47:21.659 INFO  - Administration server port 8777.

That’s weird. It should be the same. I always start stuff by hand because I run openelec (which is very limited). What kind of OS are you running on your Pi?

i use Raspbian GNU/Linux 8 \n \l
For testing i set the whole directory to chmod -R 777

ok i found the problem but not the solution. if i start /etc/init.d/blynk start for example from the folder/tmp. It didn’t start correctly. He can’t find the propertie Files, becouse it seems the server.jar search in the actually folder which i started the command. if i type the command /etc/init.d/blynk start from the folder /opt/blynk, everything works very well.
I think thats the problem. i must start the server.jar in that folder in which the server.jar is.But how can i start it on raspberry pi startup?

The script should handle that for you, the COMMAND var has the full path for all vars.

Are you using full paths everywhere?

i think there is a bug in server.jar. for testing i modified the server.properties

form
#folder for logs.
logs.folder=./logs

to

#folder for logs.
logs.folder=/opt/blynk/logs

i started the script directly in folder /opt. But blynk still touch the folder logs in /opt/logs. Thats wrong!

So i think this is a bug.

I’m having the same issue, weird enough. When I run the script from a different dir, the files are indeed elsewhere.

The reason for that is very simple. You need always run jar with

java -jar server

command as documentation states.

COMMAND=“java -jar /storage/blynk/server.jar -dataFolder /storage/blynk”

should be

cd /storage/blynk/
java -jar server.jar -dataFolder /storage/blynk"

This is how it was done initially. But I agree this is not always clear. So I made some changes. Please try
Dropbox - Error - Simplify your life this build and tell me if it works for you. Also have in mind that default logs path is “./logs” where “.” is dir from which you are running script.

1 Like

cool. it works!!! now i can start my script in /opt and the server is running correctly. Many thanks to you!!!
Tom

2 Likes

May I ask are you running Blynk on an Openelec system? As you say the changes you can make are very limited when using Openelec. Maybe you just meant you generally start scripts manually in Openelec but not in reference to Blynk. If you are running Blynk with Openelec could you provide a few details of the set up.

Sure, I put the Blynk files on the /storage part and create the startup script manual. I also run it manual because my Pi is basically my 24/7 media machine, so it’s nearly always running.

Because the server side is Java-based it’s pretty much the only thing running beside the XBMC part of course.

There is a binary Java which you can download, add it to your path and you’re done.

Thanks I will give it a try when I have time.
If a local server can be run with Openelec presumably Blynk can also run with Blynk servers (rather than local servers), is this right?

Yes. You need to choose which one is preferred for you. Here is some thoughts when to use local.

Thanks Dimitriy but just to be clear my question is not about local and cloud based servers. It is about the ‘locked down’ media player software Openelec on the Raspberry Pi. I am aware it can all be done with Raspbian or RaspBMC but not quite sure about the best media player, in my opinion, Openelec.

Lichtsignaal has indicated it can be done so I will try to set it up when I have time.
Maybe I will wait until you support Python and then I could consider building a Kodi AddOn for Blynk control.

Just let me know if you need assistance. So far it’s running quit smooth here with one exception. When I start copying files things seem to freeze up because all the CPU is used up. This is partly because I have an older model Pi, with the newer ones this shouldn’t be a problem.

On average when watching some stuff on the Pi the Java server works very good :slight_smile:

Merry Christmas everyone.

Lichtsignaal with reference to Openelec and the message I sent you about connecting to the Blynk server rather than having the server on a Raspberry Pi running Openelec.

Looking at the various scripts for different operating systems I notice there is some ‘experimental’ Python modules available. Having previously written some very basic Kodi AddOns then I believe it should be possible to access the Blynk server with an Openelec system.

Do we have any Kodi coders on the site or has anyone already used the Python stuff?

I’d love to give it a go, but I’m afraid I never used Python (how hard can it be though … ) but I never made stuff for Kodi, so I’m all for a solution, but I’m already spending way too much time in the attic according to my wife :wink:

I took a look at some of the Python stuff that comes with Blynk earlier today.

I was quite pleased to be able to control my central heating.

Turn ON:
python blynk_ctrl.py --token=xxxxxxxxxxxxxxxxxxxxxxxxxxxx -vw 11 1

Turn OFF
python blynk_ctrl.py --token=xxxxxxxxxxxxxxxxxxxxxxxxxxxx -vw 11 0

v being virtual, w being write, 11 being the pin number and 1 and 0 high and low (on / off)

Blynk provides the following info for blynk_ctrl.py

usage: blynk_ctrl.py [-h] [-t TOKEN] [-dw [PIN [VAL …]]]
[-aw [PIN [VAL …]]] [-vw [PIN [VAL …]]] [-dr PIN]
[-ar PIN] [-vr PIN] [–delay SECs] [–delayAll SECs]
[-s SERVER] [-p PORT] [–target TOKEN] [–dump]

I’m guessing this is just working locally at the moment but I will investigate further and report back. When I say locally I don’t mean a local server, simply the device being controlled is local to the device running Python.