# TIP: Send SMS alerts when emerge completes

## timeBandit

My most recent large update (>500 packages) prompted this idea. Around the tenth time an ebuild failed mere moments after I left the room (not to return for hours), I decided to have the darn thing alert me when it wanted attention.

The following assumes you have installed:The mailx client (mail-client/mailx), to send mail from shell scripts and the command line.An MTA (mail transfer agent) for mailx to use, such as Postfix, Sendmail or ssmtp.

This script is intended to run in a pipeline after emerge and send a short message to explain why emerge stopped.

```
#!/bin/sh

# Record exit status of emerge--must precede ALL other commands!

[ $? -eq 0 ] && RESULT="PASS" || RESULT="FAIL"

# Change next line to your mobile/SMS address

SMS_ADDR="2015551212@sms.some-mobile.com"

LAST_PKG="$(tac /var/log/emerge.log | awk '$2 == ">>>" {print $4,$5,$6,$7; exit;}')"

CFG_MODS="$(find /etc -iname '._cfg????_*' | wc -l)"

mail -s"Emerge Completed" $SMS_ADDR <<-EOM

   ${RESULT} ${LAST_PKG}

   :::

   ${CFG_MODS} config updates pending

   $*

EOM
```

Change SMS_ADDR to your phone's SMS address and be sure to mark the script executable. You might also want to make it readable only by root if you need to guard your mobile number.

Usage is simple:

```
emerge -uDv world; emobalert
```

That's it. Notice you run the script unconditionally after emerge so it can detect the exit status (that is, use the semicolon, never && or || or any other pipe connector).

When emerge exits, you'll receive a text message similar to this:

```
From: root@localhost.localdomain

(Emerge Completed)

PASS (2 of 2) app-crypt/seahorse-2.22.3

:::

0 config updates pending
```

The message body reports the last ebuild attempted, whether the overall merge succeeded or failed and whether you need to apply any configuration file updates. Any command-line arguments supplied to emobalert are appended to the message.

On those rare occasions when you have better things to do than watch compiler output scroll by, this trick helps you still catch problems quickly.

----------

## Bones McCracker

Very nice.  Thank you for sharing it.

I had thought about dropping a .forward file with my sms address during emerges, but even with "email_summary", it's far too much data.  This captures the important tidbits.

----------

## YourNameInHere

what sms gateway do you use? Is there something for free to use? 

thx

YourNameInHere

----------

## Bones McCracker

my cell phone provider has a gateway

----------

## timeBandit

 *BoneKracker wrote:*   

> my cell phone provider has a gateway

 Likewise.

For US Verizon Wireless numbers the gateway address is number@vtext.com for text, number@vzwpix.com for multimedia messages. I think most carriers provide a similar service (in the US at least). Check the support section of the carrier's website or call customer service.

----------

## comprookie2000

Thanks works great!

I found my cell phone providers number here;

http://en.wikipedia.org/wiki/SMS_gateway#Carrier-provided_SMS_to_email_gateways

----------

## Bones McCracker

 *comprookie2000 wrote:*   

> Thanks works great!
> 
> I found my cell phone providers number here;
> 
> http://en.wikipedia.org/wiki/SMS_gateway#Carrier-provided_SMS_to_email_gateways

 

You might want to only send a few until you get a bill, so you can make sure they're not charging you (or see how much they're charging you).

----------

## milomak

This is very interesting the same has happened to me very time so I will be interested to see if I can implement it.

----------

## .yankee

Nice trick... But isn't it easier to use --keep-going emerge parameter?

----------

## Bones McCracker

 *.yankee wrote:*   

> Nice trick... But isn't it easier to use --keep-going emerge parameter?

 

Yeah.  Or you could just use Ubuntu.   :Razz: 

----------

## coolsnowmen

 *BoneKracker wrote:*   

>  *.yankee wrote:*   Nice trick... But isn't it easier to use --keep-going emerge parameter? 
> 
> Yeah.  Or you could just use Ubuntu.  

 

You do know you are on forums.gentoo.org right?

----------

## timeBandit

 *.yankee wrote:*   

> Nice trick... But isn't it easier to use --keep-going emerge parameter?

 The two are not mutually exclusive.

----------

## Bones McCracker

 *coolsnowmen wrote:*   

>  *BoneKracker wrote:*    *.yankee wrote:*   Nice trick... But isn't it easier to use --keep-going emerge parameter? 
> 
> Yeah.  Or you could just use Ubuntu.   
> 
> You do know you are on forums.gentoo.org right?

 

You do know that was joke, right?

----------

## polovinamozga

great script!

----------

## .yankee

 *timeBandit wrote:*   

>  *.yankee wrote:*   Nice trick... But isn't it easier to use --keep-going emerge parameter? The two are not mutually exclusive.

 

True... Though, if I understand the script well, it seems that the informative value of running it after emerge -keep-going would be very small. Did you think about modifying it so that it would parse the log more thoroughly? I guess it wouldn't be that difficult to detect, how many packages were skipped during such emerge job.

----------

## timeBandit

 *.yankee wrote:*   

> True... Though, if I understand the script well, it seems that the informative value of running it after emerge -keep-going would be very small. Did you think about modifying it so that it would parse the log more thoroughly? I guess it wouldn't be that difficult to detect, how many packages were skipped during such emerge job.

 Agreed, but I have no need. I am not too fond of --keep-going, I prefer to fix problems as they arise (as you might have guessed  :Wink: ). When I have some time I'll look into counting failed packages. I have an idea for a simple solution but need to experiment.

Thanks for all the compliments and comments so far.  :Smile: 

----------

## BonezTheGoon

This is one of the coolest things I have ever seen someone contribute!  I really would like to see this suggested as a feature update to portage so that you can just define an address in your /etc/make.conf and have it done every time without remembering to run the script.  Have you submitted it as a feature request?  I'd certainly vote for it's injection!!

Awesome work!  Thanks so much for sharing!!

----------

## bobspencer123

Hey this looks like a great little script. Thanks so much for your effort.

I was wondering if this could be edited to include the ability to send an email about a package failure. I do have a cell phone but I'm a cheap sob and don't have unlimited text.

----------

## lazy_bum

 *bobspencer123 wrote:*   

> Hey this looks like a great little script. Thanks so much for your effort.
> 
> I was wondering if this could be edited to include the ability to send an email about a package failure. I do have a cell phone but I'm a cheap sob and don't have unlimited text.

 

Sure you can. I've made some quick and ugly (and it is *ugly*) hack some days ago to send also latest logfile path (this could be edited to send ie. tail -100 failed.log).

```
#!/bin/sh

# Record exit status of emerge--must precede ALL other commands!

[ $? -eq 0 ] && RESULT="PASS" || RESULT="FAIL"

# Change next line to your mobile/SMS address

SMS_ADDR="your@mail"

LAST_PKG="$(tac /var/log/emerge.log | awk '$2 == ">>>" {print $4,$5,$6,$7; exit;}')"

CFG_MODS="$(find /etc -iname '._cfg????_*' | wc -l)"

LOG_PKG="$(ls -l -rt /var/log/portage/ | grep -v elog | tail -1 | awk '{print $9}')"

mail -s"Emerge Completed" $SMS_ADDR <<-EOM

   ${RESULT} ${LAST_PKG}

   :::

   see latest logfile /var/log/portage/${LOG_PKG}

   $*

EOM

```

----------

## timeBandit

 *BonezTheGoon wrote:*   

> Awesome work!  Thanks so much for sharing!!

   :Very Happy:  Thanks! Sorry for not responding sooner, this thread fell off my egosearch due to age.

 *bobspencer123 wrote:*   

> I was wondering if this could be edited to include the ability to send an email...

 Er...that's what it does.  :Wink:  Conversion to SMS happens at the carrier's gateway address, not in the script. Just change SMS_ADDR to the email address where you want to send the note.

Note: The script is partially broken as of Portage 2.1.6.13 and later (at least in the 2.1.x series). The exit status of emerge no longer reflects the pass/fail status of the last ebuild. As a result the message sent reports PASS even if an ebuild failed. The log excerpt will still clue you in if there was a failure (e.g., "(5 of 17) app-who/badwolf").

I'll look into it soon and either find a more reliable test or file a bug on Portage (if warranted).

----------

## zeroplus

 *bobspencer123 wrote:*   

> Hey this looks like a great little script. Thanks so much for your effort.
> 
> I was wondering if this could be edited to include the ability to send an email about a package failure. I do have a cell phone but I'm a cheap sob and don't have unlimited text.

 

I realize this is a bit late but in my frustration with waiting for undefined amounts of time led me to make a similar script  :Smile:  "run_with_email_confirm" (a bit of a mouthful i know heh).

```
#!/bin/bash

if [ $# -lt 1 ]; then

   echo "Usage: $0 <COMMANDS>"

   exit 1;

fi

OUTPUT_TEMP_FILE=/etc/output.log

echo RUNNING:

count=1

for arg in "$@"; do

   echo "#${count}: ${arg}"

done

for arg in "$@"; do

   $arg | tee ${OUTPUT_TEMP_FILE}

   EXIT_STATUS=${PIPESTATUS[0]}

   if [ $EXIT_STATUS -ne 0 ] ; then

      COMMAND="${arg}"

      RESULT=$EXIT_STATUS

      OUTPUT=$(tail -n 30 "${OUTPUT_TEMP_FILE}") 

      echo "EXIT STATUS: ${EXIT_STATUS}"

      email_results "${COMMAND}" "${RESULT}" "${OUTPUT}"

      rm "${OUTPUT_TEMP_FILE}"

      exit ${EXIT_STATUS}

   else

      echo "EXIT STATUS: ${EXIT_STATUS}"

   fi   

done

email_results "${FULL_COMMAND}" 0

rm "${OUTPUT_TEMP_FILE}"

exit 0

```

If you read through the script closely you'll notice that I'm both printing to the console and saving STDOUT and STDERR to a log file.

The script basically does this:

take commands ie:

```
run_with_email_confirm "emerge --sync" "emerge -u world"
```

separate the commands (each of which is just an argument)

run them one at a time

if it passes then run the next command (if it passes all command arguments, then send an email with a PASS and indicate the commands run)

if it fails then take the command that failed ${arg}, exit code ${PIPESTATUS[0]}, last 30 lines of console output ${OUTPUT_TEMP_FILE} and pass it to the email script "email_results" which basically makes it pretty.

The script "email results"[/list] is as follows:

```

#!/bin/bash

if [ $# -lt 2  ] || [ $# -gt 3 ]; then

   echo "Usage: $0 <COMMAND> <RESULT> <OUTPUT: optional>"

   exit 1;

fi

EMAIL="<your email goes here>"

COMMAND=$1

EXIT_STATUS=$2

if [ $2 -eq 0 ]; then

   RESULT="PASS"

else

   RESULT="FAIL"

fi

OUTPUT=$3

{

echo "<table>"

echo "<tr>"

echo "<td width=\"100\"><b>DEVICE</td><td>: </td></b> <td>$HOSTNAME</td>"

echo "</tr>"

echo "<tr>"

echo "<td width=\"100\"><b>COMMAND</td><td>: </td></b> <td>$COMMAND<br></td>"

echo "</tr>"

if [ $EXIT_STATUS == 0 ]; then

   echo "<tr>"

   echo "<td width=\"100\"><b>RESULT</td><td>: </td></b> <td>PASS</td>"

   echo "</tr>"

   echo "</table>"

else

   echo "<tr>"

   echo "<td width=\"100\"><b>RESULT</td><td>: </td></b> <td>FAIL</td>"

   echo "</tr>"

   echo "<tr>"

   echo "<td width=\"100\"><b>CODE</td><td>: </td></b> <td>$EXIT_STATUS</td>"

   echo "</tr>"

   echo "</table><br>"

   echo -e "<td><b>OUTPUT:</b><br><br>"

   echo "$OUTPUT<br>"

fi

} | mutt -e "set content_type=text/html" -s "$HOSTNAME: $RESULT notification" $EMAIL

```

I apologize for all the hack html tags in there...there's probably a better way to handle it.  The email wouldn't take terminal color codes etc since it's not a terminal interpretting them so I had to go with html.

You can follow this link to set up gentoo to send from your own gmail address:

http://en.gentoo-wiki.com/wiki/Gmail_and_sSMTP

The result of all this would look something like this:

DEVICE       : <HOSTNAME>

COMMAND   : <COMMANDS_RUN> (or the command that it failed at)

RESULT       : PASS or FAIL

CODE          : 141

OUTPUT:

<the console output is shown here only if the thing failed>

Maybe this'll be useful to someone else in the future.  The code could be cleaned up indeed etc but hey, it works.  I'm working on an embedded device (Beagleboard-XM) so I have to leave it plugged in on top of the router and I can't monitor it 24/7 over serial console right.

UPDATE: I was stupid and forgot that my $RESULT variable is actually the same as $EXIT_STATUS after I changed the script up at one point; meaning...$RESULT is pretty useless..meh

----------

## ppurka

 *timeBandit wrote:*   

>  *BonezTheGoon wrote:*   Awesome work!  Thanks so much for sharing!!   Thanks! Sorry for not responding sooner, this thread fell off my egosearch due to age.
> 
>  *bobspencer123 wrote:*   I was wondering if this could be edited to include the ability to send an email... Er...that's what it does.  Conversion to SMS happens at the carrier's gateway address, not in the script. Just change SMS_ADDR to the email address where you want to send the note.
> 
> Note: The script is partially broken as of Portage 2.1.6.13 and later (at least in the 2.1.x series). The exit status of emerge no longer reflects the pass/fail status of the last ebuild. As a result the message sent reports PASS even if an ebuild failed. The log excerpt will still clue you in if there was a failure (e.g., "(5 of 17) app-who/badwolf").
> ...

 It might be overkill for something so simple, but my script (see my signature below) does keep track of the number of failed packages and the number of skipped packages. You could hook up your script to be executed at the end of the emerge process (via a config variable POST_CMD). Since your script will be run from inside my script, it will actually be able to access the internal variables of my script, in particular the array exit_status which contains the number of failed and skipped packages.

----------

## midnightnerd

Hey, I get that this is an old topic but has it been submitted as a feature request to Portege?

----------

## John R. Graham

No real need, in my opinion. A trivial wrapper script or bash alias will integrate this little script into your normal Portage workflow.  :Wink: 

- John

----------

