Created
October 8, 2023 06:53
-
-
Save gegere/e5712479ba35b984ebae56d36115b22b to your computer and use it in GitHub Desktop.
System Update Script / Send to Slack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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