Wednesday, August 5, 2009

Automatically run a command after waking up from Sleep/Standby

I came across a situation recently where I needed to have my Windows system run a command automatically every time the system woke up from Sleep/Standby/Hibernation: a custom monitor colour calibration profile was lost and needed to be explicitly restored every time the system woke up (see previous post.)

In my quick research, by no means exhaustive, I found three ways of doing this under Windows:

  1. If you're (un)lucky enough to run Vista, you can set up a Task Scheduler job that reacts to a System Event
  2. Alternatively, you can install a Windows utility whose exact purpose in life is to run stuff whenever you suspend or resume the system.
  3. You can write a simple tool, service, or Windows script to respond to and process the Windows power events, and do whatever you need to to.
In either case, the idea is that you will run a tool or script or batch file that you create and control, and you get the system to run it.

In my case, the machine is running Vista so I first explored option 1. For my needs, it works very well. Under Vista only (as far as I can tell this is not possible under earlier versions of Windows) here are the steps to create a Task Scheduler entry to run when the system resumes:
  • Run "TASKSCHD.MSC" to bring up the Task Scheduler
  • From the Task Scheduler Library actions choose "Create Task"
  • Fill in the General information as needed
  • Under Triggers, choose New and Begin the task "On an event"
  • Set basic settings Log:System, Source:Power-Troubleshooter and Event ID:1
  • Click OK and go to Actions
  • Set Action:Start a program, and enter the location of your script/tool
  • Review the Conditions and Settings tabs and adjust as needed

Once this is done, save the task and test it by choosing "Run" with the item selected. Once this is working, test it by suspending then resuming your system, it should work!

Another alternative for all versions of Windows is Hibernate Trigger. It is a simple tool that is easy to install and configure, and doesn't require any additional information here really. Basically:

  • Create your script, e.g. C:\WAKEUP.BAT
  • Install and run Hibernate Trigger
  • Configure it to run C:\WAKEUP.BAT on Resume
The third option is if you want to roll your own tool (similar to Hibernate Trigger probably) that responds to the Windows power events directly and does what you want. I have not investigated this option since the first two alternatives meet my needs, but there are two main options you can use:


So those are a few solutions to meet the need of running tasks after system sleep. Most people don't need to do thise, but those that do it's a good trick to know.

Make a Spyder2express color profile "stick" after standby/sleep

I recently bought a new Thinkpad X200s and it's quite nice. One symptom that affects many notebooks, perhaps exacerbated by newer LED backlights now in frequent use, is that LCD displays have a very "cold" or "blue" tinge. Apparently this makes them look "better" in store compared to older, yellowing screens; anyway...

As any photo or design pro (which I am neither) will tell, doing any photo processing on a screen that has incorrect colors leads to bad results. For very "cool" screens, this results in garish and darkened flesh tones, and a some colour over-saturation. Since I will be on the road with this laptop and taking pictures, I decided to get a basic hardware screen colour calibrator, the Sypder2express from Datacolor which is under $100.

Now for my basic needs, this simple unit (pro units can cost hundreds or thousands) worked as advertised and as designed, and the software is kept up to date (Vista x64 support was a download away.) The problem that became apparent immediately was that the new colour settings in Windows were LOST every time the system resumed from sleep/standby.

According to Datacolor's support response (prompt, if a bit flippant) was that this is a Windows problem, not a product problem, and that they had a software workaround in, you guessed it, only in their more expensive products.

Not surprisingly, many tools out there are available that can help without a costly upgrade. The one I ended up using was xcalib, a command-line calibration tool for Windows and X. To solve my problem, I configured Windows to invoke on Resume, a script that runs the following commands (after copying xcalib.exe from the download link into my PATH):

  1. xcalib -clear
  2. xcalib
The first command clears any existing color settings in your video card. The second one (re)applies your default color calibration settings that the Spyder2express tool generated and installed into Windows.

Alternatively, you can configure the second command to run "xcalib " to apply a specific color profile. For example, to explicitly apply the Spyuder2express color setting installed into Windows, you would run:

  1. xcalib -clear
  2. xcalib c:\windows\system32\spool\driver\color\Spyder2express.icm
I found that for some reason, xcalib would on occasion not apply the color file properly. Adding the "-clear" command first fixed this; it may not be necessary on all systems.

To figure out how to configure Windows to run a script on Resume (after Sleep/Standby/Hibernate) see my next blog post. In my case, I created a "WAKEUP.BAT" system batch script and added the xcalib commands to it.