Friday, December 23, 2011

How to schedule a program to run on an event

This post explains how to make a task run when a certain event happens on your computer. The basic concept is that Windows 7 has an event log which logs all kinds of things that happen. Many are informational, but there may be other situations such as errors that you will need to start a task when it happens using the task scheduler.

Why in the world would I need to do this?

The main reason I looked into this issue is because I had a RAID 1 (disk mirroring) setup that I wanted to make sure had not failed. RAID 1 or disk mirroring is a way to keep your computer up and running if a hard drive fails. In essence, you have two drives of equal size and whenever the computer writes to one hard drive, it writes the identical information to the other hard drive. If one of the drives fails or has an error, the system will use the remaining drive to run your computer.

The problem was that Windows 7 does not notify you if one of the drives fail or if there is a problem with the mirroring! Since it doesn't notify, you may have a false sense of security until BAM, the other drive goes out and your system is down and you lose data and time.

My solution was that if the mirror fails there is an event written to the system log with a unique error code. I used the task scheduler so that anytime the error is written to the system log, it would run a program to send an email letting me know.

How do I set it up?

Note: These instructions assume that you have some technical knowledge about Windows 7 such as using the event viewer. These things will not be explained in this post, but if you have questions I will try to answer them.

So I can show an actual example, I will use my situation of sending an email when a Windows 7 RAID 1 mirror fails, but the principle can be applied to any kind of system log message.

1. Follow the directions for setting up automated email using SendEmail in a previous blog post. Test to make sure you can send emails successfully.

2. Simulate the event. Open the event viewer and find the error or information code you want the program to trigger on.

3. Right click on the event and click “Attach Task to this Event”.

4. The task create window pops up. Fill in the following:

Type a description and hit <next>

Hit <next> again to skip the “When Event is Logged” screen.

Click the radio button “Start a Program” and hit <next>.
Type in “sendemail” Do not put in the path.

In the “Add Arguments” box, type in your batch file arguments such as “-f <from.address@domain.com -t to.address@domain.com -u Test -m Test -s smtp.gmail.com:587 -o tls=yes -xu <google.userid@domain.com> -xp <yourpassword>”

In the “Start in” box, type the path to the program file without quotes such as C:\Program Files (x86)\SendEmail. Hit <next>.

Click the “Open Properties dialog for this task when I hit Finish”.

Hit “Finish”.

In the Properties dialog, click the radio button that says “Run whether user is logged on or not”.
Change the “Configure for:” drop down to Windows 7.

On the “Settings” tab, click the “Run task as soon as possible after scheduled start is missed”.

Press “OK”.

If you have a username/password on your system, a Windows 7 authentication screen pops up. Fill it in with your username and password.

5. Run the task and make sure it works. You can do this by right clicking the task and clicking on run. Be sure to check to be sure the task doesn’t get stuck.

6. Simulate the failure to get the event id to trigger and make sure it works for real.


What else can task scheduling be used for?

The task scheduler is versatile so it can be used for a number of purposes. You can set a task to run based on a schedule (i.e., daily, weekly, monthly, etc.), one time only, when the computer starts, when you log on and of course when a specific event is logged.

There are many tasks that can be done which can be found with a Google search. Some simple examples would be that perhaps you want to defragment your hard drive at 3 AM or even make an alarm clock that plays music to wake you up at a certain time.

Task scheduling can be very useful to automate your system.

Are there any better tutorials for using the task scheduler?

What? You don't like this one? Actually, I do realize that these instructions are limited by the fact that my target audience is a more technically inclined with computers (i.e., geeks - lol). For a more basic understanding of task scheduling, check out the links below:

(Ummm... well, right now I don't have any links. Know of any? Let me know and I'll take credit for it... errr... I mean I will post it here. Hopefully I'll dig up some soon!)

I think that you have errors in this post!

You are probably right! There are lots of nuances and details on how things work in Windows 7 and I certainly don't know them all. If you think something is wrong, please post. I will investigate and if I am wrong, hang my head in shame and correct the mistake. If I am correct, I will ridicule you in public! (lol - not really :-P )

How to send email from a command line using GMAIL

These instructions tell how to set up a way to send email messages from your computer using a program called SendEmail (http://goo.gl/VnaFh) with GMAIL from a command line.

Why would you want to do this?

The main reason would be to let the computer notify you of conditions or issues if they occur. I originally used this program to create an automated notification if my RAID 1 mirroring failed because Windows does not notify you if your mirror fails, but it can be used for any purpose. It is easy to set up a task that if a certain error occurs it will fire off an email. I'll explain how to do this in another post.

How does it work?


Below is a bit of information as to what is happening with the sendemail program and how it works with GMAIL, but I won’t go into extensive details. As Heinlein said, “Never worry about theory as long as the machinery does what it's supposed to do.”


SendEmail is program that lets you send email from a command line. It supports Authentication using Transport Layer Security (TLS) which is one of the security protocols that GMAIL uses (the other is Secure Sockets Layer or SSL). GMAIL requires authentication using one of the two protocols.


To set up SendEmail, make a directory such as “C:\Program Files (x86)\SendEmail” and put the sendemail.exe program inside. It is a stand alone program so no installation is required.


The general settings for gmail:


SMTP server: smtp.gmail.com
TLS port: 587
SSL port: 465
Username: <myusername@gmail.com>
Password: <password>


Note that the SMTP port is not standard (i.e., port 25) and is different for TLS and SSL.

Can I use another email service besides GMAIL?

Yes as long as the email service will let you send email through its SMTP server and if it uses authentication, uses the TLS security protocol. You would have to look up similar information as listed above for your email service and then put them in the SendEmail command line (see below). If anyone happens to successfully use another email service, please post it in the comments and I will add it to the blog.

How do I use SendEmail?


To use SendEmail, here is the basic syntax for using it with GMAIL:
sendemail
-f <from address>
-t <to address>
-u <subject>
-m <message>
-s smtp.gmail.com:587
-o tls=yes
-xu <username@gmail.com>
-xp <password>


You can find other settings by running sendemail from a command line with no options and it will bring up the help screen.


To test, open a cmd window and run the program with the options you want. Here is an example:


sendemail -f <from.address@domain.com> -t <to.address@domain.com> -u Test -m Test -s smtp.gmail.com:587 -o tls=yes -xu <google.userid@domain.com> -xp <yourpassword>

Notes:


1. There is also a SendSMS program by the same author that lets you send text messages via command line. I haven’t tried it, but it should be straightforward.

2. I am not sure if you have an subject or body with spaces if you have to put quotes around it. When I find out I'll update.

3. The sendemail program will also let you send an attachment. I haven't had a reason to do so, but the functionality is there.

Welcome to the Windows 7 Notebook of Commander Dave!

The purpose of this blog is to document some of the things I have found out about Windows 7, some of which isn't readily available in a Google search. It will include settings, programs, how-to, quirks and solutions. Most of the things I will post involves things I have come across myself, but will occasionally point to outside sources such as howtogeek.com.


Some things get a little technical and some things will be simple tweaks, so pick and choose as you desire.


Hope this blog will help someone find a solution to some obscure problem!


-Commander Dave