Skip to content

Instantly share code, notes, and snippets.

@gegere
Created October 8, 2023 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gegere/e5712479ba35b984ebae56d36115b22b to your computer and use it in GitHub Desktop.
Save gegere/e5712479ba35b984ebae56d36115b22b to your computer and use it in GitHub Desktop.
System Update Script / Send to Slack
#!/bin/bash
# Replace 'YOUR_SLACK_TOKEN' with your Slack Bot Token
SLACK_TOKEN="ABC123"
# Replace 'YOUR_CHANNEL_ID' with the ID of the Slack channel where you want to send the message
CHANNEL_ID="C123456789ZYX"
LOG_FILE="/var/log/update_script.log"
# Get the start date
START_DATE=$(date)
# Function to log messages to console and log file
log_message() {
local message="$1"
echo "$message"
echo "$message" >> "$LOG_FILE"
}
# Initialize OUTPUT with the start date
OUTPUT="Running System Update Script at $START_DATE"
log_message "$OUTPUT"
if APT_UPDATE_OUTPUT=$(apt-get update 2>&1); then
OUTPUT="$OUTPUT\nUpdate successful\n$APT_UPDATE_OUTPUT"
log_message "$OUTPUT"
else
OUTPUT="$OUTPUT\nUpdate failed\n$APT_UPDATE_OUTPUT"
log_message "$OUTPUT"
exit 1
fi
if APT_UPGRADE_OUTPUT=$(apt-get upgrade -y 2>&1); then
OUTPUT="$OUTPUT\nUpgrade successful\n$APT_UPGRADE_OUTPUT"
log_message "$OUTPUT"
else
OUTPUT="$OUTPUT\nUpgrade failed\n$APT_UPGRADE_OUTPUT"
log_message "$OUTPUT"
exit 1
fi
if APT_AUTOCLEAN_OUTPUT=$(apt-get autoclean 2>&1); then
OUTPUT="$OUTPUT\nAutoclean successful\n$APT_AUTOCLEAN_OUTPUT"
log_message "$OUTPUT"
else
OUTPUT="$OUTPUT\nAutoclean failed\n$APT_AUTOCLEAN_OUTPUT"
log_message "$OUTPUT"
exit 1
fi
# Get the end date
END_DATE=$(date)
# Append OUTPUT with the end date
OUTPUT="$OUTPUT\nSystem Update Script completed at $END_DATE"
# Send the output to Slack
curl -X POST -H "Content-type: application/json" \
-H "Authorization: Bearer $SLACK_TOKEN" \
-d "{\"channel\":\"$CHANNEL_ID\",\"text\":\"$OUTPUT\"}" \
https://slack.com/api/chat.postMessage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment