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 < -t -u Test -m Test -s -o tls=yes -xu <> -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 )

No comments:

Post a Comment