Skip to content

Instantly share code, notes, and snippets.

@gilangvperdana
Created June 20, 2022 14:46
Show Gist options
  • Save gilangvperdana/727529165e7766fabcbcd72b0ab95e6b to your computer and use it in GitHub Desktop.
Save gilangvperdana/727529165e7766fabcbcd72b0ab95e6b to your computer and use it in GitHub Desktop.
Alert Linux when Boot & Shutdown

Alert Linux when Boot & Shutdown

Environment

  • BOT Telegram
  • Linux

Make Telegram Service

You can follow this Step

Start Alert on Boot

  • Make an simple Script
nano /usr/bin/boot
#!/bin/bash

ALERT=$(echo -e "\u26A0")
DATE=$(date)
HOSTNAME=$(hostname)

telegram "$ALERT $HOSTNAME ALREADY RUNNING on $DATE $ALERT"
chmod +x /usr/bin/boot
  • Make an crontab
crontab -e
@reboot /usr/bin/boot
sudo systemctl enable cron.service

Before Shutdown

  • Make Simple Script
nano /usr/bin/beforeshutdown
#!/bin/bash

ALERT=$(echo -e "\u26A0")
DATE=$(date)
HOSTNAME=$(hostname)

telegram "$ALERT $HOSTNAME are Turning to POWEROFF State on $DATE $ALERT"
chmod +x /usr/bin/beforeshutdown
  • Make Systemd Service
nano /etc/systemd/system/run-before-shutdown.service
[Unit]
Description=Alert at shutdown
DefaultDependencies=no
Before=shutdown.target

[Service]
Type=oneshot
ExecStart=/usr/bin/beforeshutdown
TimeoutStartSec=0

[Install]
WantedBy=shutdown.target
systemctl daemon-reload
systemctl enable run-before-shutdown.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment