Skip to content
  • PC & Mobile
    • Windows
    • Mac
    • Linux
    • Mobile
    • Android
    • iOS
    • Chrome OS
  • Hardware
    • PC Hardware
    • Product Review
    • Smart Home
  • Lifestyle
    • Internet
    • Gaming
    • Productivity
    • How Things Work
    • Glossary
    • News
  • Cheatsheets
  • Deals

Dark mode:

Home > Linux

Have Linux Send You an Email Notification After Task Completion

Richa
Richa
Apr 11, 2014

In this article we’re going to take a look at how you can have Linux trigger an email alert once a certain condition has been met. We’re assuming you’re an intermediate or advanced user – which you should be, if you’re looking to set up email alerts on Linux – and that you have the mail client installed already.

Having an alert sent to you through email can be very useful. For example, if you have a large build being done in the background, it makes no sense to wait around for the process to finish. In such cases, you can have Linux send you a message once the build is done. It saves you a lot of time. You can also have an email alert sent to you at a certain time. If you have an engagement in the evening, you can program it into Linux so you don’t forget!

First, let’s take a quick look at how you can send an email to yourself. The command for it is “mailx” or “mail“. This command may be different on your system, depending on the version of Linux you’re running (and it will only work if the server has been setup properly). The command syntax is:

mailx –s "your subject" recipient@provider.com <<< "your message"

You can also attach a file to this, if you want

mailx –s "your subject" recipient@provider.com < filename.txt

EmailAlert1

Also read:9 of the Best Email Clients for Linux9 of the Best Email Clients for Linux

Getting Linux to Trigger an Email Alert

Now, we can couple the command we used above with a conditional statement. This means that when the condition is satisfied, an email will be sent to you. Enter the following in the command line:

condition_when_done date | mailx –s "subject" youremail@provider.com <<< "your message"

This can be done in another way too. Let’s look at another example. What if you were waiting for a server to respond? You can either keep typing commands in the shell or you can have an email alert delivered to you when the server wakes up. You can use this command, for example:

while :; do date; ping –c1 servername && break; \done; mail

If you look closely, the code above is an infinite loop. When your system pings the server (servername), the loop gets broken and a mail will be sent to you. You don’t need to wait until a server has booted up or updated.

If you’re worried about system resources being spent, you can put the system to sleep by amending the command a little:

while :; do date; ping –c1 servername && break; sleep 200; \done; mail

In this case, the computer will sleep for 200 seconds before it executes a new cycle. You also get a regular alert (that the loop is working) every few seconds this way.

You can put virtually any shell command as the condition (so long as it’s sensible and doesn’t overload your system):

while :; do date; the_command_to_be_executed && break; \done; mail

What if you wanted to email yourself on a certain date at a certain time? You can change the command above a little like this:

while [ date +"%T" -gt '06:00:00']; do date; the_command_to_be_executed && break; \done; mail

If you were looking to receive notifications whenever you receive an email on your Linux system, you can install applications that support email notifications. Two of the more popular applications are PopTray Minus and Mail Notification.

You can experiment with the commands given above. The premise is simple – just design a condition statement which, when met, will trigger an email alert. If you need more info or help on how to use mailx, simply type “mailx --help“to display the help list.

Image Credit: tux flag linux penguin red waving , gray mail envelope white postal letter, memory reminders reminder dimensional control

Is this article useful?
Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

  • Facebook
  • Tweet

Popular Posts

How to Build and Install a Custom Kernel on Ubuntu

The Beginner's Guide to Git

How to Fix Can't Type in Terminal Issue in Linux

How to Utilize Python for Basic Linux System Administration and Networking Tasks

How to Use the G++ Compiler on Linux

How to Use tcpdump for Packet Capture

9 of the Best Email Clients for Linux

How to Generate SSL Certificates on Linux Using OpenSSL

How to Use the Nessus Vulnerability Scanner on Linux

How to Use Emacs for RSS with Elfeed

Affiliate Disclosure: Make Tech Easier may earn commission on products purchased through our links, which supports the work we do for our readers.

Read Next

How to Build and Install a Custom Kernel on Ubuntu
The Beginner’s Guide to Git
How to Use Sed in Linux for Basic Shell Tasks
5 Tools to Easily Create a Custom Linux Distro
How to Efficiently Share or Send Google Docs via Email
What Is the Matrix Protocol and How to Install It
Why and How to Edit Your Sudoers File in Linux
How to Spot a Facebook Phishing Email and Similar Scams

© 2022 Uqnic Network Pte Ltd.
All rights reserved.

  • About
  • Contact
  • Advertise
  • Write For Us
  • Terms of Use
  • Privacy Policy
  • RSS Feed Terms
Do not share my Personal Information.