Add uptime and/or a daily fortune to your email signature
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.
Let's now test the commands we will be using. From your terminal prompt issue the command:
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:
and you should see something like:
Cheer Up! Things are getting worse at a slower rate.
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:
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:
- Removes the previous ~/.sig file to start fresh.
- Creates a new, empty ~/.sig file.
- Adds my name to the beginning of the ~/.sig file.
- Appends the output of the fortune command under last line.
- 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.
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:
and add a line like:
* * * * * Â /path/to/script
Where /path/to/script is the explicit path to your script you created.
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