Blynk Server Bash Updater Script

Hi, blynkers, especially local server guys.

  • script should be stored in folder with server-*.jar and run.sh;
  • script autodetect java version;
  • script update server version in run.sh file;

I`am a beginner at bash script writing, together we could improve this script( post useful edits in this topic ).Code:

#!/bin/bash
#
# Perform Blynk server update.

#######################################
# Colorize your stdout
#######################################
#normal=$(tput sgr0)                      # normal text
normal=$'\e[0m'                           # (works better sometimes)
bold=$(tput bold)                         # make colors bold/bright
red="$bold$(tput setaf 1)"                # bright red text
green=$(tput setaf 2)                     # dim green text
fawn=$(tput setaf 3); beige="$fawn"       # dark yellow text
yellow="$bold$fawn"                       # bright yellow text
darkblue=$(tput setaf 4)                  # dim blue text
blue="$bold$darkblue"                     # bright blue text
purple=$(tput setaf 5); magenta="$purple" # magenta text
pink="$bold$purple"                       # bright magenta text
darkcyan=$(tput setaf 6)                  # dim cyan text
cyan="$bold$darkcyan"                     # bright cyan text
gray=$(tput setaf 7)                      # dim white text
darkgray="$bold"$(tput setaf 0)           # bold black = dark gray text
white="$bold$gray"                        # bright white text

#######################################
# Clear last terminal line
#######################################
clearLastLine() {
    tput cuu 1 && tput el
}

# Script Begin #
echo "${white}Checking for updates...${normal}"

serverUrl=$(curl -s https://api.github.com/repos/blynkkk/blynk-server/releases/latest \
| grep "browser_download_url.*.jar")

# Detect java version
javaVersion=$(java -version 2>&1 | grep -i version | cut -d'"' -f2 | cut -d'.' -f1-2)
if [ "$javaVersion" == "1.8" ]; then
    serverUrl=$(echo "$serverUrl" | grep ".*-java8.*")
else
    serverUrl=$(echo "$serverUrl" | grep -v ".*-java8.*")
fi

# BlynkServer latest version Url
serverUrl=$(echo "$serverUrl" | cut -d : -f 2,3 \
| tr -d \")

serverName=${serverUrl##*/} # BlynkServer latest version Filename

if [ -f $serverName ]; then
    echo "${green}You running latest version.${normal}"
else
    echo "${yellow}Downloading $serverName...${normal}"

    wget -q $serverUrl --show-progress
    if [ $? == 0 ]; then # If wget response is 200
        clearLastLine
        echo "${green}Download completed.${normal}"
    else
        clearLastLine
        echo "${red}Download failed.${normal}"
    fi

    if [ -f $serverName ]; then
        # Setting permissions for downloaded new BlynkServer
        echo "${white}Setting 644 permissions.${normal}"
        chmod 644 $serverName

        # Update run.sh file
        echo "${white}Updating run.sh.${normal}"
        sed -i "s/server-.*jar/$serverName/g" run.sh

        echo "${yellow}Restarting Blynk server.${normal}"
        # Kill old BlynkServer.
        kill $(ps aux | grep -v grep | grep 'server-.*jar' | awk '{print $2}')
        # Run new BlynkServer
        sudo ./run.sh &
    fi
fi

Thanks for helpful advices or editings: distans.

5 Likes

I wrote a script like yours about a year ago I think, but yours looks a bit more elegant t.b.h. Main difference is I just rename the server-version.jar to server.jar so I can have my run.sh static and don’t have to modify it :slight_smile:

1 Like

I think many blynkers wrote a script like this, but it should be written single time and well. Also modifying run.sh has advantages as keeping versioning: ease roll-back( in case old server jar not removing ); compatibility with blynk server setup instructions.

True, I did keep old .jar files, but my script was more dirty, I’ll admit that, lol. It’s probably posted somewhere on the forum if you search for it.

In the common quest for the ultimate install/update/start/stop-script I have some feedback :slight_smile:

I think the server-[version]-java8.jar is for RPi and the server-[version].jar for Linux in general. So a slight difference between those two which people might not notice. I want to use the latter :slight_smile:

It’s ā€œbad practiceā€ and actually no reason at all to set the permissions to -rwxrwxrwx. Default -rw-r--r-- will do just fine. ā€œjava -jarā€ is what’s executing and just needs to read the *.jar file. Also -R changes permissions recursively and has no effect on a single file :wink:

1 Like

Well, you lost me after feedback :stuck_out_tongue:

I just manually log in with putty, grab the latest file with wget (using past ā€˜remembered’ CLI commands and adjusting for file name), stop the server edit the rc.local to match the new file name and run rc.local again. Usually done in about 30-45 seconds… not bad I think for a Linux n0ob. And it keeps me a bit more cognizant of some basic commands (not that I can remember them without looking them up… Each. And. Every. Time… arrgg…).

I was just pointing out that there is a difference between the two jars. The reason I found out is this:

CPU Support - not all Pi models
Java 9 takes advantage specific CPU instruction sets commonly found in modern 64-bit processors, like the ARM v7/8 CPUs and x86 range. This move will help improve runtime performance.

However, as of today, this does limit Java 9 to the Raspberry Pi 2 and Pi 3 boards. Owners of model As, Bs and Zeros, with their older ARM v6 processor chips, will therefore have to keep using Java 8.

Here’s a beautiful command if you have jq (commandline JSON processor) installed:

~$ curl -s https://api.github.com/repos/blynkkk/blynk-server/releases/latest | jq --raw-output '.assets[1] | .browser_download_url' | wget -i -

Saving to: "server-0.29.2.jar"

If you change the .assets[1] to .assets[0] you’ll get the server-[version]-java8.jar instead.

Dude! You can’t type that fast! :rofl:

But if I use BOTH index fingers I can do a fair clip :smiley:

Thankfully the CLI seems to remember my past commands… stored in some file if I recall correctly… I just arrow up and down, find the command, edit, press enter, rinse & repeat :stuck_out_tongue:

if you enter the command ā€œhistoryā€ you can see a list of all past commands :slight_smile:

Say you forgot the curl command, you can search in the list using grep:

history | grep curl

That’ll search for curl in that list.

The chmod is a very good notice. You might want to add inputting a parameter to let the script choose for downloading the Java9 or 8 version, e.g. update.sh java8

Thanks, code in header updated.:kissing:

1 Like

I only have 2000 lines in my history file so that method has it limitations :wink: Yes, I know I can change that…

@fragolinux uses lsb_release -rs to check which Java version that should be installed in his script from the thread Completely unattended local Blynk-Server setup which is one way to do it (But you’ll get the *-java8.jar version either way because there was only one release when he wrote it :wink: ).

rc.local is obsolete :stuck_out_tongue: Say hello to systemd!

Added java version autodetection. Code in header updated.

fragolinux script is for a complete new installation on a virgin system, that’s why he checks against the distro.

He looks in /etc/os-release after ā€œ*BIANā€ and if the release version is >9 with lsb_release -rs. If so, Java 8 gets installed. But does all RPi distros have ā€œ*BIANā€ as ID? Never mind. It’s not important! :stuck_out_tongue:

I might start working on a install/update script, but for Linux only. It’s complicated enough without having to guess what the RPi’s needs :smiley:

For those interested, I made a small how-to for systemd. If someone could test if it works on RPi it would be great! :slight_smile:

2 Likes