自動下載 node.js
又來分享腳本了,node.js 的更新速度異常地快,我想可能是因為用了 CI 自動部署了吧。

為了迎合這種奇特的生命周期,這個腳本可以自動找到更新,下載回來,然後發出通知:
( rev3 26.Oct.2016 )
#!/bin/bash

# rev 3

LVAR="$HOME/local/var/nodejs_sha"
RUNTIME="$HOME/local/nodejs"
TOAST="$HOME/local/autorun/toastme"

DOWNLOADS="$HOME/Downloads"
LATEST=https://nodejs.org/dist/latest

TEMP=$( mktemp )

function __o { rm $TEMP; }
trap __o EXIT

ORHS_SHA=$( cat $LVAR )

Try=0

while [ $Try -lt 10 ]; do
    wget -qO- $LATEST/SHASUMS256.txt > $TEMP

    FILE_NAME=$( grep linux-x64.tar.gz $TEMP | awk '{ print $2 }' )
    TARGET="$LATEST/$FILE_NAME"
    LHS_SHA=$( grep linux-x64.tar.gz $TEMP | awk '{ print $1 }' )

    Try=$(( $Try+1 ))

    if [ "$LHS_SHA" == "" ]; then
        sleep 10
        continue
    fi
    break
done

if [ "$LHS_SHA" == "" ]; then
    echo "Cannot get SHA, exiting"
    exit 1
fi

if [ "$LHS_SHA" == "$ORHS_SHA" ]; then
    exit
fi

wget -qO- $TARGET > "$DOWNLOADS/$FILE_NAME"
RHS_SHA=$( sha256sum "$DOWNLOADS/$FILE_NAME" | awk '{ print $1 }' )

if [ "$LHS_SHA" == "$RHS_SHA" ]; then
    echo -n "$LHS_SHA" > $LVAR

    cd "$RUNTIME"
    tar zxf "$DOWNLOADS/$FILE_NAME"
    ODIR=$( echo $FILE_NAME | rev | cut -c 8- | rev )
    NDIR=$( echo $FILE_NAME | grep -o "v[0-9]\+\.[0-9]\+\.[0-9]\\+" )
    mv "$ODIR" "$NDIR"

    echo "Discovered new release. Dispatching notifications ..."
    $TOAST "Auto node.js" "A new version of node.js $NDIR has been released and downloaded"
else
    echo "Wrong SHA sum, discarding the file"
fi

term-notify

通知利用的是 term-notify 這個自製 app ,雖然自己做的東西有點自賣自誇,不過最近伺服器也開始穩定下來了,可以像這樣收到通知真的很方便呢~
#!/bin/bash

# toastme <title> <message>

# Registered receipient
RCPTS=(
    "<UUID1>"
    "<UUID2>"
    "<UUID3>"
)

# Go!
for RCPT in ${RCPTS[@]}; do
    curl https://notify.astropenguin.net/ \
        --data-urlencode "id=$RCPT" \
        --data-urlencode "action=deliver" \
        --data-urlencode "title=$1" \
        --data-urlencode "message=$2"
done

systemd

最近喜歡用 systemd ,跟 crontab 比起來, systemd 的彈性較高。

nodejs-download.service
[Unit]
Description=Auto download new nodejs release

[Service]
ExecStart=/home/penguin/local/autorun/download-node
Type=oneshot
Restart=no

[Install]
WantedBy=multi-user.target

Timer 設定每三個小時執行一次。
nodejs-download.timer
[Unit]
Description=Run nodejs-download.service every 3 hours

[Timer]
OnCalendar=00,03,06,09,12,15,18,21:00:00
Unit=nodejs-download.service

[Install]
WantedBy=timers.target

最後執行這兩包指令就大公告成了!
systemctl enable nodejs-download.timer
systemctl start nodejs-download.timer
Tag(s): systemd bash
Profile picture
斟酌 鵬兄
Tue Aug 30 2016 03:51:38 GMT+0000 (Coordinated Universal Time)
Last modified: Wed Oct 26 2016 09:04:27 GMT+0000 (Coordinated Universal Time)
Comments
No comments here.
Do you even comment?
website: 
Not a valid website
Invalid email format
Please enter your email
*Name: 
Please enter a name
Submit
抱歉,Google Recaptcha 服務被牆掉了,所以不能回覆了