81 lines
1.9 KiB
Plaintext
81 lines
1.9 KiB
Plaintext
|
to get notification from your linux machine
|
||
|
after installing binwiederhier/ntfy docker container
|
||
|
-------------------------------------------
|
||
|
|
||
|
1. sudo nano /home/user/scripts/ntfy_script.sh
|
||
|
|
||
|
2. past this code :
|
||
|
|
||
|
#!/bin/bash
|
||
|
|
||
|
# Function to check if the machine is up and running
|
||
|
check_machine_status() {
|
||
|
# Ping Google's DNS server as a test
|
||
|
ping -c 1 8.8.8.8 > /dev/null
|
||
|
if [ $? -eq 0 ]; then
|
||
|
echo "Machine is up and running."
|
||
|
return 0
|
||
|
else
|
||
|
echo "Machine is down."
|
||
|
return 1
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
# Function to get public IP address
|
||
|
get_public_ip() {
|
||
|
public_ip=$(curl -s https://api.ipify.org)
|
||
|
echo "Public IP address: $public_ip"
|
||
|
}
|
||
|
|
||
|
# Function to get local IP address
|
||
|
get_local_ip() {
|
||
|
local_ip=$(hostname -I)
|
||
|
echo "Local IP address: $local_ip"
|
||
|
}
|
||
|
|
||
|
# Main script
|
||
|
while true; do
|
||
|
if check_machine_status; then
|
||
|
# Get current date and time
|
||
|
current_datetime=$(date +"%Y-%m-%d %H:%M:%S")
|
||
|
|
||
|
# Get public and local IP addresses
|
||
|
get_public_ip
|
||
|
get_local_ip
|
||
|
|
||
|
# Send notification using curl
|
||
|
message="Your machine is up and running at $current_datetime | Public IP: $public_ip | Local IP: $local_ip"
|
||
|
curl -d "$message" https://ntfy.sahab.eu.org/asus
|
||
|
break
|
||
|
else
|
||
|
sleep 10 # Sleep for 10 seconds before checking again
|
||
|
fi
|
||
|
done
|
||
|
|
||
|
|
||
|
----------------------------------------
|
||
|
3. sudo nano /etc/systemd/system/ntfy.service
|
||
|
|
||
|
4. [Unit]
|
||
|
Description=Notification service for machine status
|
||
|
After=network.target
|
||
|
|
||
|
[Service]
|
||
|
Type=simple
|
||
|
ExecStart=/bin/bash /home/user/scripts/ntfy_script.sh
|
||
|
|
||
|
[Install]
|
||
|
WantedBy=multi-user.target
|
||
|
|
||
|
|
||
|
|
||
|
5. sudo systemctl daemon-reload
|
||
|
6. sudo systemctl enable ntfy.service
|
||
|
sudo systemctl start ntfy.service
|
||
|
|
||
|
7. sudo systemctl status ntfy.service
|
||
|
|
||
|
8. crontab -e
|
||
|
|
||
|
9. @reboot sleep 60 && /bin/bash /home/user/scripts/ntfy_script.sh
|