Skip to content

Instantly share code, notes, and snippets.

@LaggAt
Forked from outadoc/pushbullet.sh
Created October 4, 2017 14:48
Show Gist options
  • Save LaggAt/6125e64fe753fa84f4521475f7de50e3 to your computer and use it in GitHub Desktop.
Save LaggAt/6125e64fe753fa84f4521475f7de50e3 to your computer and use it in GitHub Desktop.
Pushbullet bash script
#!/bin/bash
# from outadoc/pushbullet.sh
# Changes:
# get ACCESS_TOKEN from /etc/pushbullet.conf <LaggAt>
# optional wait for process ending (3rd parameter with PID/executable name), before sending with exit code information <LaggAt>
if [ $# -eq 0 ]; then
echo "Usage: $0 <message> [<title> [pid]]"
exit
fi
MESSAGE=$1
TITLE=$2
if [ $# -lt 2 ] || [ -z "$2" ]; then
TITLE="`whoami`@${HOSTNAME}"
fi
if ! [ -z "$3" ]
then
PID=$3
re='^[0-9]+$'
if ! [[ $3 =~ $re ]] ; then
PID=`pidof -sx $3`
fi
EXITCODE=$(strace -q -e none -e exit_group -p $PID 2>&1 | grep '+++' | tail -n 1 | sed -En 's/[^0-9]*([0-9]{1,})[^0-9]*/\1/gp')
TITLE="$TITLE, PID $PID exited with $EXITCODE"
fi
source /etc/pushbullet.conf
curl -s -u $ACCESS_TOKEN: -X POST https://api.pushbullet.com/v2/pushes --header 'Content-Type: application/json' --data-binary '{"type": "note", "title": "'"$TITLE"'", "body": "'"$MESSAGE"'"}' >/dev/null 2>&1
@LaggAt
Copy link
Author

LaggAt commented Oct 4, 2017

What it does

It uses free Pushbullet service (https://www.pushbullet.com/) to send notifications from bash. I mostly use it for long-running processes, but it could be used in surveillance systems or just anything you want to be informed of immediatly.

This script is just an easy way to send notifications. It also has the feature to wait for another process to finish, even if it's alredy running.

Usage Examples

pushbullet.sh "Hello World"
pushbullet.sh "Text" "Title"
pushbullet.sh "" "" #empty title shows "user@host"

Wait for a Process to end, then notify

I often start a process, without thinking how long it would take. I just hit "make" and wait for it to finish. Some of these turn out to run fairly long, now I can fire up another shell, and register to get notified when the previous startet make finishes.

For this we use a 3rd parameter. You may leave the other two parameters empty, using the defaults.

pushbullet.sh "" "" 3456
pushbullet.sh "" "" make #this uses pidof to find a make process

Installation

replace YOUR_TOKEN from https://www.pushbullet.com/#settings/account - "Access Tokens"

echo 'ACCESS_TOKEN="YOUR_TOKEN"' | sudo tee /etc/pushbullet.conf
cd /usr/bin
sudo wget https://gist.github.com/LaggAt/6125e64fe753fa84f4521475f7de50e3/raw/pushbullet.sh
sudo chmod +x pushbullet.sh

@LaggAt
Copy link
Author

LaggAt commented Oct 5, 2017

If you know you're starting a long running task, try this:

./rebuild -j4 ; pushbullet.sh "Done. Exited $?"

./rebuild is the long running process, ";" executeds the right hand command always regardless of the previous command failed, then we send "$?" thru pushbullet, which shows the exit code on your phone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment