Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fragolinux/a5e7d6153afab4c52d91d41ff9860b8c to your computer and use it in GitHub Desktop.
Save fragolinux/a5e7d6153afab4c52d91d41ff9860b8c to your computer and use it in GitHub Desktop.
#!/bin/bash
# Starts and stops Blynk-Server
# /etc/init.d/blynk
### BEGIN INIT INFO
# Provides: Blynk-Server
# Required-Start: $syslog
# Required-Stop: $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Blynk-Server initialisation
### END INIT INFO
# how to use:
# sudo wget --no-check-certificate -O /etc/init.d/blynk https://gist.githubusercontent.com/fragolinux/a5e7d6153afab4c52d91d41ff9860b8c/raw
# sudo chmod 755 /etc/init.d/blynk && sudo update-rc.d blynk defaults
# sudo /etc/init.d/blynk install
# credits: startup and update script based on https://github.com/WhizzCat/blynk_scripts
# on latest line of server.properties add the account (email address) you will create and then use on Blynk App, to restrict creation ONLY to that account - useful on public VPS servers
# you need to create a Gmail account and put it's address and password in latest lines of mail.properties file: http://docs.blynk.cc/#blynk-server-advanced-local-server-setup-enabling-sending-e-mails-from-local-server
# DON'T USE your regular Gmail account, create one ad hoc, because you NEED to enable "less secure apps" to have it working with Blynk: https://support.google.com/accounts/answer/6010255?hl=en
# these settings are NEEDED to receive emails with tokens to be used in Blynk sketches
# info on how to access your LOCAL server from Blynk App: https://www.hackster.io/rei-vilo/private-iot-with-blynk-on-local-server-619926
BASE=/opt/Blynk
PID=$BASE/blynk.pid
LOG=$BASE/blynk.log
# ERROR=$BASE/blynk-error.log
COMMAND="$(which java) -jar $BASE/BlynkServer.jar"
status() {
echo
echo "==== Status of Blynk-Server"
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 Blynk-Server"
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 BlynkServer.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 "==== Stopping Blynk-Server"
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
}
install() {
echo "==== Installing Latest Blynk-Server"
source /etc/os-release
OPSYS=${ID^^}
DISTRO=$(/usr/bin/lsb_release -rs)
# install java
if [[ $OPSYS == *"BIAN"* ]] && [[ $DISTRO == *"9."* ]]; then
sudo apt-get -y update
sudo apt-get -y install oracle-java8-jdk
# sudo apt-get -y install software-properties-common dirmngr
# sudo add-apt-repository "deb http://ppa.launchpad.net/webupd8team/java/ubuntu yakkety main"
# sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C2518248EEA14886
else
echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu xenial main" | sudo tee /etc/apt/sources.list.d/webupd8team-java.list
echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu xenial main" | sudo tee -a /etc/apt/sources.list.d/webupd8team-java.list
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EEA14886
sudo apt-get -y update
echo debconf shared/accepted-oracle-license-v1-1 select true | sudo debconf-set-selections
echo debconf shared/accepted-oracle-license-v1-1 seen true | sudo debconf-set-selections
sudo apt-get -y install oracle-java8-installer
fi
# download latest Blynk Server jar file, using curl and jq (commandline JSON processor), used to extract url from Github latest releases page, and curl
sudo apt-get -y install jq curl
mkdir $BASE
cd $BASE
curl -s https://api.github.com/repos/blynkkk/blynk-server/releases/latest | jq --raw-output '.assets[0] | .browser_download_url'|wget -i - -O BlynkServer.jar
# download addons config files
wget https://raw.githubusercontent.com/blynkkk/blynk-server/master/server/core/src/main/resources/server.properties
wget https://raw.githubusercontent.com/blynkkk/blynk-server/master/server/notifications/email/src/main/resources/mail.properties
# install openssl, used to generate self-signed certificates, and generates them not interactively and passwordless
sudo apt-get -y install openssl
openssl req -x509 -nodes -days 3650 -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com" -newkey rsa:2048 -keyout server.key -out server.crt
openssl pkcs8 -topk8 -nocrypt -inform PEM -outform PEM -in server.key -out server.pem
# set Blynk working folder to the one created just a few lines back (~/Blynk), and certificates
sed -i 's#data.folder=#data.folder=/opt/Blynk#g' server.properties
sed -i 's#server.ssl.cert=#server.ssl.cert=/opt/Blynk/server.crt#g' server.properties
sed -i 's#server.ssl.key=#server.ssl.key=/opt/Blynk/server.key#g' server.properties
echo "Now you can reboot..."
echo "Admin interface will be on https://$(hostname -I|tr -d " "):9443"
echo "Your initial Admin login email is admin@blynk.cc"
echo "Your initial Admin password is admin"
}
update() {
echo "==== Updating to Latest Blynk-Server"
source /etc/os-release
OPSYS=${ID^^}
DISTRO=$(/usr/bin/lsb_release -rs)
cd $BASE
mv -f BlynkServer.jar BlynkServer.jar.old
curl -s https://api.github.com/repos/blynkkk/blynk-server/releases/latest | jq --raw-output '.assets[0] | .browser_download_url'|wget -i - -O BlynkServer.jar
}
case "$1" in
'start')
start
;;
'stop')
stop
;;
'restart')
stop ; echo "Sleeping..."; sleep 1 ;
start
;;
'status')
status
;;
'install')
install
;;
'update')
stop ; echo "Sleeping..."; sleep 1 ;
update ;
start
;;
*)
echo
echo "Usage: $0 { start | stop | restart | status | install | update }"
echo
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment