One of the essential tasks for developers and sysadmin is to get an alert notification about failed services or running out of disk space and other critical failures. Let us see how to send or push a direct message to a mobile device powered by Apple iOS or Google Android phone.
How to push/send message to iOS and Android in real time
We can use the AWS SNS push notification service to send an alert directly to an application on a mobile device. However, today I am going to use straightforward app service called Pushover. It is a simple app to get real-time notifications on Android, iPhone, iPad, and Desktop, including Android Wear and Apple Watch.
Why send notifications?
As an independent developer and Linux sysadmin, I needed a simple way to get a notification for my side projects using API. I can get a notification for failed backups or my Nginx service overloaded due to problems or MySQL read-only replica running out of sync. My search ended with Pushover. However, it is not a free service. To use Pushover for yourself or a small group, it’s just a USD $5 one-time purchase on each platform. You can send 7,500 messages per month, which is more than sufficient for my needs. They also have an option to send messages for a group of developers and IT teams. My criteria were simple:
- I needed support for my Perl, Python, and bash/shell scripts.
- Must push notification to iPhone.
- Must not be very expensive.
The Pushover service seems to fit all my requirements. Enough talk, let’s get our hands-on dirty with some examples.
Step 1 – Sign up for Pushover
First download Pushover device clients:
- Android version
- iOS (iPhone, iPod Touch, and iPad) version
Make sure you subscribe to service or get a seven-day trial account. Once logged in, register your cli application, set its name, and get an API token in return.
Step 2 – Create a shell script wrapper API script
Create a new shell script as follows:$ vi ~/bin/cli_app.sh
Append the code:
#!/usr/bin/env bash # push/send message ios and android using API from my Linux box # Set API stuff _token='YOUR-API-TOKEN-HERE' _user='YOUR-USER-NAME-HERE' # Bash function to push notification to my iPhone # yes you can push/send message android too using the same function push_to_mobile(){ local t="${1:cli-app}" local m="$2" [[ "$m" != "" ]] && curl -s --form-string "token=${_token}" --form-string "user=${_user}" --form-string "title=$t" --form-string "message=$m" https://api.pushover.net/1/messages.json } |
Next, use the source command:$ source ~/bin/cli_app.sh
Test it:$ push_to_mobile "bash-notification" "This is a test. Job foo failed on $HOSTNAME @ $(date)"
Immediately I will get a notification on my phone:
How to Send both email and push notifications to your phone from script
We can simple use the sendmail command or mail command as follows from our shell scripts:
#!/usr/bin/bin bash # Wrapper backup-script.sh by Vivek Gite under GPL v2.0+ # ------------------------------------------------------- # # Set email stuff # warning: must need pre-configured smtpd such as sendmail/postfix # subject="rsnapshot backup job failed at $HOSTNAME" log_file="/path/to/my.log.txt" from="webmaster@cyberciti.biz" to="webmaster@cyberciti.biz" # # start daily backup and set log to ${log_file} # all reports created by rsnapreport.pl script including ERROR # /usr/bin/rsnapshot daily 2>&1 | /root/bin/rsnapreport.pl > "${log_file}" # Catch errors status=$? alogs="$(egrep -w '^ERROR:|ERROR' $log_file)" # If failed, notify using both email and push message to my phone if [ $status -ne 0 ] || [ "$alogs" != "" ]; then mail -A "$log_file" -s "$subject" -r "$from" "$to" <<< "Backup script failed with error. Check attached log file" # # Push/send message to iOS and Android using Pushover API too # source ~/bin/cli_app.sh push_to_mobile "backup-script" "$subject. See $to email for detailed failed log." >/dev/null fi |
See the following tutorials for more information about sending emails from the CLI:
Conclusion
So far, Pushover service and app worked great for me. I can easily send/push messages to iOS and Android devices using shell/Perl/Python scripts. Check out all other suggestions on my Twitter thread:
We use a paid solution (APIs/Apps) to push critical messages regarding IT infrastructure (servers/VMs/routers/firewalls) to sysadmin and ops teams iOS/Android phones. Are there any *free* Apps/services for *personal* projects to push notifications in real-time to iOS/Android?
— The Best Linux Blog In the Unixverse (@nixcraft) June 1, 2020