Add uptime and/or a daily fortune to your email signature

Jack Wallen
Dec 16, 2009
Updated • Dec 28, 2012
Linux
|
5

Today's Linux article has two purposes: 1) To add a little spice to your boring old email signature, and to help you understand a bit about bash scripting. The goal is to be able to create a pseudo-dynamic email signature that adds the uptime of your Linux box and/or the output of the fortune command. With this you can get creative and make your email signature as unique as you like.

In order for the bash script we will create to work, a single application will have to be installed and the users' crontab will have to be edited. These are simple tasks and the end result is sure to raise your "geek cred". No, this will not make your systems run more efficiently, your code compile perfectly, or your breath smell deliciously.  But hopefully you will learn something and impress your friends and family with your trickery.

What to install

It used to be Linux distributions would ship with the Fortune application. That is no longer the case. You will have to install Fortune in order to take advantage of this simple little app. But what does it do? All Fortune does is print a random adage from stored flat-files found in /usr/share/games/fortune. You can add to these files by opening them up and editing them. But first you have to install the application. Since we are going to be dealing with the command line (for bash scripting), let's install from command. Open up a terminal window and issue the following command:

sudo apt-get install fortune

And fortune will be installed.

Testing

Let's now test the commands we will be using. From your terminal prompt issue the command:

uptime

and you should see something like:

11:13:45 up 225 day(s), 3 users, load average 0.07, 0.07, 0.02

Now let's take Fortune out for a test. Issue the command:

fortune

and you should see something like:

Cheer Up! Things are getting worse at a slower rate.

The script

For this script we are going to take advantage of the echo command and we will write to the ~/.sig file, which will then be used for the signature file in your email client. The basic script looks like:

#! /bin/bash

fortune=/usr/games/fortune
rm ~/.sig
touch ~/.sig
echo 'jack wallen' >> ~/.sig
echo `$fortune` >> ~/.sig
echo 'uptime:'  `uptime` >> ~/.sig

NOTE: There are two different "quotes". The first is the single quote in the fourth line. The second is the back-tick in the fifth and sixth lines. These back-ticks are necessary to indicate a command is in use.

This script does the following:

  1. Removes the previous ~/.sig file to start fresh.
  2. Creates a new, empty ~/.sig file.
  3. Adds my name to the beginning of the ~/.sig file.
  4. Appends the output of the fortune command under last line.
  5. Appends the string "uptime" (no quotes) followed by the output of the uptime command under the last line.

Create this file (you'll want to personalize the echo 'jack wallen' >> ~/.sig line of course) and save it in a convenient directory. You will then need to give this file executable permission, so issue the following command:

chmod u+x FILENAME

Where FILENAME is the name of the script.

Cron

Now we need to create a cron job that will run the script so that the file isn't static. Open up your crontab editor with the command:

crontab -e

and add a line like:

* * * * *  /path/to/script

Where /path/to/script is the explicit path to your script you created.

Email

Every email client will be configured differently. But basically all you need to do is go to the signature preferences section and configure your client to use ~/.sig as the file for your email signature. Now every minute that file will be different and your signature will be "dynamic", containing a random quote and your machine's uptime.

Advertisement

Previous Post: «
Next Post: «

Comments

  1. Hobo said on March 3, 2015 at 9:36 am
    Reply

    You need a 2nd executable to generate the sig file. If you create the executable in the .sig file, obviously it will overwrite itself …

  2. Don Birdsall said on December 17, 2009 at 8:39 pm
    Reply

    Jack,

    Did you test this?

    The script seems to be self destructive. The ‘touch’ command creates a new file but it is empty.

    The ‘echo’ commands put text into it, but it is no longer a script.

    I like the idea, but this does not work.

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

We love comments and welcome thoughtful and civilized discussion. Rudeness and personal attacks will not be tolerated. Please stay on-topic.
Please note that your comment may not appear immediately after you post it.