Linked-in profile Github profile Twitter profile Curriculum vitae
Categories: Blockchain
Created
27th August 2022
Modified
12th September 2022

Geth (go-ethereum) service auto-updater

Automate client updates, before you forget about them. It’s a good idea to keep the network fresh.

Here is a Bash script to update a hypothetical service, named geth. Scheduled it to run every few days, in the early hours of the morning.

The script will get the latest geth binary, compare the version output with whats currently installed before proceeding, establish the new binary, and reload service. Note that you may need to run this script with elevated privileges.

A Gist of this script can be found at on GitHub

#!/bin/bash

# Geth auto-update
# Installs and reloads `geth` service, if version is different
# Assumes you have installed and are running a systemd service
# `/etc/systemd/system/geth.service`
# 
# [Unit]
# Description=Go Ethereum Client
# After=network.target
# Wants=network.target
# [Service]
# User=geth
# Group=geth
# Type=simple
# Restart=always
# RestartSec=45
# ExecStart=/usr/local/bin/geth ..."
# [Install]
# WantedBy=default.target

GETH_BUILD_PATH=~/go/bin/geth
GETH_DEPLOY_PATH=/usr/local/bin/geth

# Get latest geth binary
go install github.com/ethereum/go-ethereum/cmd/geth@latest

# Compare versions to determine continuation
GETH_OLD_VERSION=`${GETH_DEPLOY_PATH} version | sed -n '2p'`
GETH_NEW_VERSION=`${GETH_BUILD_PATH} version | sed -n '2p'`

# Exit, nothing has changed
if [ "$GETH_NEW_VERSION" == "$GETH_OLD_VERSION" ]; then
	echo "Nothing to update for geth.";
	exit 0;
fi

echo "Current ${GETH_OLD_VERSION}";
echo "New ${GETH_NEW_VERSION}";

# Remove old binary
echo -n "Removing old geth... "
rm -f $GETH_DEPLOY_PATH
echo "OK"

# Deploy new binary
echo -n "Installing new geth... "
mv $GETH_BUILD_PATH $GETH_DEPLOY_PATH
echo "OK"

# Ensure new binary executable
echo -n "Make new geth executable... "
chmod +x $GETH_DEPLOY_PATH
echo "OK"

# Ensure SELinux context for systemd
TEST_SELINUX=`sestatus | grep mode`
if [${#TEST_SELINUX} -gt 0 ]; then
	echo -n "Give geth bin_t context... "
	chcon -R -t bin_t $GETH_DEPLOY_PATH
	echo "OK"
fi

# Restart Geth
echo -n "Restart geth service... "
service geth restart
echo "OK"

exit 0;

Leave a Reply

Your email address will not be published. Required fields are marked *