I’ve put together a Bash script to simplify updating the znnd
binary for the go-zenon
service on Linux systems. This script automates downloading the latest binary, verifying its integrity, stopping the service, updating the binary, and restarting the service. Here’s a step-by-step guide on how to use it.
What the Script Does
- Ensures the script is run as root (required for service management and file operations).
- Removes any existing temporary file to avoid conflicts.
- Downloads the znnd-linux-amd64.tar.gz binary (version 0.0.8-private) from the HC1 repo
- Verifies the downloaded file’s SHA-256 checksum for security.
- Stops the
go-zenon
service. - Extracts the binary and renames it from
znnd-linux-amd64
toznnd
. - Installs the binary to
/usr/local/bin/znnd
, overwriting any existing file. - Sets executable permissions.
- Restarts the go-zenon service.
- Cleans up temporary files.
The Script
Save the following code to a file named update_znnd.sh
:
#!/bin/bash
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo "This script must be run as root"
exit 1
fi
# Define expected checksum
EXPECTED_CHECKSUM="4bee99d2f69b71e22f5b29bc6d1866f9672bbe002487209447306c518ee8336b"
DOWNLOAD_URL="https://github.com/hypercore-one/go-zenon/releases/download/v0.0.8-private/znnd-linux-amd64.tar.gz"
TEMP_FILE="/tmp/znnd-linux-amd64.tar.gz"
# Log memory usage before any operations
echo "Initial memory usage:"
free -h
# Remove existing file if it exists
if [ -f "$TEMP_FILE" ]; then
echo "Removing existing file at $TEMP_FILE"
rm "$TEMP_FILE"
fi
# Download the binary
echo "Downloading znnd binary..."
wget -O "$TEMP_FILE" "$DOWNLOAD_URL"
# Check if download was successful
if [ $? -ne 0 ]; then
echo "Failed to download binary"
exit 1
fi
# Verify checksum
echo "Verifying checksum..."
COMPUTED_CHECKSUM=$(sha256sum "$TEMP_FILE" | cut -d ' ' -f 1)
if [ "$COMPUTED_CHECKSUM" != "$EXPECTED_CHECKSUM" ]; then
echo "Checksum verification failed!"
echo "Expected: $EXPECTED_CHECKSUM"
echo "Got: $COMPUTED_CHECKSUM"
rm "$TEMP_FILE"
exit 1
fi
echo "Checksum verified successfully"
# Check if go-zenon service is running
echo "Checking go-zenon service status..."
if ! systemctl is-active --quiet go-zenon; then
echo "go-zenon service is not running, no need to stop"
else
# Log memory usage before stopping service
echo "Memory usage before stopping go-zenon service:"
free -h
# Stop go-zenon service
echo "Stopping go-zenon service..."
systemctl stop go-zenon
# Wait 10 seconds for the service to stop
echo "Waiting 10 seconds for go-zenon service to stop..."
sleep 10
# Check if the service is stopped
if systemctl is-active --quiet go-zenon; then
echo "go-zenon service still running, attempting to forcefully kill..."
systemctl kill go-zenon
sleep 2
if systemctl is-active --quiet go-zenon; then
echo "systemctl kill failed, attempting manual kill of znnd process..."
pkill -f znnd
sleep 2
if pgrep -f znnd > /dev/null; then
echo "Error: Failed to stop go-zenon service or znnd process"
exit 1
fi
fi
fi
echo "go-zenon service stopped successfully"
fi
# Inspect archive contents
echo "Archive contents:"
tar -tzf "$TEMP_FILE"
# Extract binary to /tmp/
echo "Extracting binary..."
tar -xzf "$TEMP_FILE" -C /tmp/
# Check if the expected binary exists
if [ ! -f "/tmp/znnd-linux-amd64" ]; then
echo "Error: znnd-linux-amd64 binary not found in archive"
exit 1
fi
# Move and rename the binary to /usr/local/bin/znnd with overwrite
mv -f /tmp/znnd-linux-amd64 /usr/local/bin/znnd
# Check if move was successful
if [ $? -ne 0 ]; then
echo "Failed to move binary to /usr/local/bin/"
exit 1
fi
# Set proper permissions
chmod +x /usr/local/bin/znnd
# Start go-zenon service
echo "Starting go-zenon service..."
systemctl start go-zenon
# Check if service started successfully
if [ $? -ne 0 ]; then
echo "Failed to start go-zenon service"
exit 1
fi
# Clean up
rm "$TEMP_FILE"
echo "Update completed successfully"
How to Use the Script
- Save the Script:
- Copy the code above into a text editor.
- Save it as
update_znnd.sh
in a convenient location (e.g.,~/update_znnd.sh
).
- Make the Script Executable:
- Open a terminal and run:
chmod +x update_znnd.sh
- Run the Script as Root:
- Execute the script with sudo:
sudo ./update_znnd.sh
- The script will provide output at each step, including download progress, checksum verification, and service status.
- Check the Output:
- If successful, you’ll see: Update completed successfully.
- If there are errors (e.g., checksum mismatch, download failure, or service issues), the script will exit with a descriptive error message.