Created
March 6, 2024 03:16
-
-
Save cipulan/a35e75989d5678976f61871a71843abf to your computer and use it in GitHub Desktop.
Run Script Before Reboot, Poweroff, or Terminated
This file contains hidden or 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
| [Unit] | |
| Description=Send Telegram Notif | |
| Requires=network-online.target | |
| After=network.target | |
| [Service] | |
| Type=oneshot | |
| RemainAfterExit=yes | |
| ExecStop=/usr/bin/bash /opt/script/down.sh | |
| [Install] | |
| WantedBy=network.target |
This file contains hidden or 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 | |
| # Get Instance Info | |
| response=$(/usr/bin/curl -s ipinfo.io) | |
| # Extract values of "ip" "region" and "org" using jq | |
| ip=$(echo "$response" | jq -r '.ip') | |
| region=$(echo "$response" | jq -r '.region') | |
| org=$(echo "$response" | jq -r '.org') | |
| # Get Instance Hostname | |
| hostname=$(hostname) | |
| name=$(echo "$hostname" | cut -d'.' -f1 | tr '[:lower:]' '[:upper:]') | |
| # Telegram Notification | |
| chat_id=-100xxxxxx | |
| bot_id=67xxxx:xxxxxxxxxxxxxmCo | |
| # Post to telegram Group | |
| /usr/bin/curl -X POST \ | |
| -H "Content-Type: application/json" \ | |
| -d '{ | |
| "chat_id": '"$chat_id"', | |
| "text": "*[ Server : '"$name"' | Status : DOWN ]*\nHostname : *'"$hostname"'* \nIP : *'"$ip"'*\nRegion : *'"$region"'*\nProvider : '"$org"' \n*<-- Please Check -->*", | |
| "parse_mode": "Markdown" | |
| }' \ | |
| https://api.telegram.org/bot$bot_id/sendMessage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run Script Before Reboot, Poweroff, or Terminated
Run script before Reboot, Poweroff, or Terminated. Additionally, it integrates with Telegram to send notifications about these events.
Usage :
sudo apt install jqordnf install jqsudo mkdir /opt/scriptsudo vi /opt/script/down.shsudo chown +x /opt/script/down.shaa-run-before-shutdown.serviceunder directory/etc/systemd/system/sudo vi /etc/systemd/system/aa-run-before-shutdown.servicesudo systemctl daemon-reloadsudo systemctl enable --now aa-run-before-shutdown.servicesh /opt/script/down.shSupported OS :
To automated this step using ansible
Reference