Showing posts with label nail. Show all posts
Showing posts with label nail. Show all posts

Tuesday, September 9, 2008

Replacing update-notifier with a cron script in Xubuntu

Update-notifier does a lot of good things. It shows you when a package manager is working in the background, it automates security updates, it caused updates to be downloaded in the background, and it provides a convenient icon to click on and launch update-manager. Good stuff.

But it's daemon eats an annoying trickle of ram and resources.

I have it set to update weekly, so the rest of the time it's just eye candy. Much of it's power is wasted on my scheduled update cycle. Time to find a replacement with less overhead.

The simple solution: Really, all I need is to run update-manager weekly. So add the following to your user crontab (crontab -e):

#Reminder bot - this e-mail replaces the update-notifier package
# nail must be instaled, it is part of the heirloom-mailx package
40 7 * * sun /usr/bin/nail -s "Reminder Bot: Run Update-Manager" me@example.com >/dev/null
or instead of using nail (to e-mail the notice), I can simply use zenity to create a pop-up:
#Reminder bot - this e-mail replaces the update-notifier package
40 7 * * sun DISPLAY=:0.0 /usr/bin/zenity --info --title "Reminder!" --text "It's time to run Update-Manager"
or zenity can also create a notification-area icon:
#Reminder bot - this e-mail replaces the update-notifier package
40 7 * * sun DISPLAY=:0.0 /usr/bin/zenity --notification --text "Reminder: It's time to run Update-Manager"

and I'll get a reminder e-mail every Sunday morning at 7:40 am...if I'm awake. Then I launch update-manager from the menu or command line...if I want.


The lazy solution: The simple solution has a drawback - none of the packages are downloaded yet, so the update might take a while (zzzzz). An additional cron entry, though, will have everything downloaded and ready.

Edit the root crontab: sudo mousepad /etc/crontab

Add the line 42 6 * * sun root apt-get update && apt-get autoclean && \apt-get -q -d -y -u upgrade (source)

This will download the updated and upgraded packages on Sunday mornings about an hour before the reminder e-mail, plenty of time.


In theory we could try 42 6 * * sun root apt-get update && apt-get autoclean && \apt-get -q -d -y -u upgrade && /usr/bin/nail -s "Reminder Bot: Run Update-Manager" me@example.com and have it all in one place, but I'll find that confusing in another 6 months.


Removing update-notifier and update-notifier-common: Nothing depends on the package in Xubuntu 8.04, so a simple sudo apt-get remove update-notifier update-notifier-common gets rid of the packages, freeing up a whopping 426kb of disk space.


I find I don't miss the old notifier at all.

Thursday, July 24, 2008

Creating an Evolution e-mail from the command line and python

Evolution is intended as a GUI e-mail client, so the scripting options are limited. Also, it looks like you cannot autosend e-mail from a script, there's so script command equivalent to the 'send' button.


But that's a good thing; I can autosend from nail when I want to. So a script will compose the e-mail, which will sit on my desktop until I review it and click to send it. Nice.


shell command source

This command creates a window with To:, From:, Subject:, and Body filled in. 'From:' is already known by Evolution, the rest is parsed from the following command:

evolution mailto:person@example.com?subject=Test\&body=Some%20crazy%20story%20stuff%0D%0Acolumn1%09%09column2%09%09column3%0D%0A%0D%0ASincerely,%0D%0A%0D%0AMe


Another (easier) way:
evolution mailto:person@example.com?cc="second_person@example.com"\&subject="This Is The Subject"\&body="This is the body of the message"\&attach=Desktop/test_file.xml
  • evolution mailto:person@example.com - Launches Evolution's e-mail composer and To: line
  • ?cc="Person@example.com" - CC: Line
  • \&subject="Subject" or ?subject="Subject" - Subject line
  • \&body="Body" - Body of the e-mail is everything after this line
  • \&attach=/path/file - Files to attach
  • %20 - Space
  • %0D%0A - CR/LF (new line)
  • %09 - Tab

python command

Python 2.x has it's own smtp module for creating e-mail, it's far more useful in most circumstances. But in this case, we want the composed evolution window.

import os
body_string = 'This is the body of the e-mail message'
body_string = body_string.replace(' ','%20')
os.popen('evolution mailto:person@example.com?subject=Test\&body=' + body_string)
  • import os - Use python's os module
  • body_string = 'This is the body of the e-mail message' - The body in normal text
  • body_string = body_string.replace(' ','%20') - Encode the spaces (evolution will decode them). Tabs, newlines, and other reserved strings need to be encoded.
  • os.popen('evolution mailto:person@example.com?subject=Test\&body=' + body_string) - The 'os.popen(cmd)' executes shell cmd. Note that cmd is just a python string, and you can use all the string tools on it, like adding body_string.
>>> import os
>>> to = 'person@example.com'
>>> cc = '"second_person@example.com"'
>>> subject = '"This is the subject"'
>>> body = '"This is the body"'
>>> attachment = 'Desktop/rss_test.xml'
>>> os.popen('evolution mailto:'+to+'?cc='+cc+'\&subject='+subject+'\&body='+body+'\&attachment='+attachment)

Sunday, June 1, 2008

Sending e-mail from the command line

I want to send automatic e-mail as a cron job from my Xubuntu 8.04 laptop. Originally, I tried routing it through the legacy mail spools - it is, after all, on the same laptop as my e-mail - but gave up in despair. Thanks to Ubuntu Forums for the info.

  1. sudo apt-get install ssmtp heirloom-mailx
    or install ssmtp and nail using Synaptic.
  2. sudo mousepad /etc/ssmtp/ssmtp.conf to edit the ssmtp config file. Modify or add the following lines to the config file:
    mailhub=mail.mymailserver.com
    AuthUser=me@mymailserver.com
    AuthPass=my_mail_password
    
    The nail config file does not need to be changed at all.
  3. Using the nail command to send e-mail:
    Tip: Pay close attention to the <RETURN>s and <CTRL+D>s
    First line: nail recipient@address.com<RETURN>
    Second line: Subject: subject<RETURN>
    Third line: E-mail body text<CTRL+D>

    Example:
    nail me@virusmagnet.com
    Subject: Lotta viruses coming into this account
    I need to stop responding to the spam.
  4. Generating e-mail as part of a script or cron job:

    Script nail -s "Test 3" recipient@email.com < /tmp/test_email will send the following e-mail.
    From: You (automatic)
    To: recipient@email.com
    Subject: Test 3
    Message Body: (read from file /tmp/test_email)


    nail -s "Test 3" recipient@email.com < /dev/null will send an e-mail with a blank body - good for quick reminders using the subject line only.

    To generate e-mail from cron events, use the command crontab -e to edit your crontab file:

    26 21 * * * nail -s "Test 3" recipient@email.com < /tmp/test_email

    This crontab entry will send the same "Test 3" e-mail every day at 9:26 p.m.

Many elements can be added or customized - this is simple enough to generate automatic e-mails to myself.

UPDATE: September 9, 2008: I could do this in evolution, but it's more fun to use cron and nail or zenity
This crontab entry:

0 6 28 * * /usr/bin/nail -s "Reminder bot: Pay The Store Rent!" me@example.com </dev/null

will send the following e-mail at 6:00 am on the 28th of each month,

From:    me <my computer>
To:      me@example.com
Subject: Reminder bot: Pay The Store Rent!
Date:    Thu, 28 Jul 2008 06:00:01 -0500 
Text:    None
So I won't forget to pay the rent.... It's important.
This sends an e-mail to a known (to nail) e-mail server, so all machines that check the account get the mail.
Alternately, if I want a a pop-up window:

0 6 28 * * DISPLAY=:0.0 /usr/bin/zenity --info --title "Reminder Bot" --text "Pay The Store Rent"

will give me a pop-up window instead, and

0 6 28 * * DISPLAY=:0.0 /usr/bin/zenity --notification --text "Reminder Bot: Pay The Store Rent"

will give me a tiny notification-area icon.