6

Possible Duplicate:
Is there any way to execute something when closing the laptop's lid?

I have a task that I want to run right before my computer sleeps or hibernates. Is there a way to schedule a task for this in the Windows 7 task scheduler?

The closest that I can see is for on locking the workstation, and this works as a surrogate, but I really don't want it to run on locking unless it is also sleeping/hibernating. Is there a way to schedule for an event like this?

fixer1234
  • 27,064
  • 61
  • 75
  • 116
Andrew Redd
  • 1,722
  • 3
  • 14
  • 13

2 Answers2

5

I think your best bet is to configure the task to shut down or hibernate your computer when it finishes, and then start the task directly instead of stopping your computer. For example, write a batch file that runs the process and then shuts down the machine, and then run that batch file.

jcrawfordor
  • 16,219
  • 5
  • 39
  • 51
  • Also see [How can I put the computer to sleep from Command Prompt/Run menu?](https://superuser.com/questions/42124/how-can-i-put-the-computer-to-sleep-from-command-prompt-run-menu) – Andreas Haferburg Sep 08 '17 at 11:25
1

Found the answer in this question which sites a technet article. The Jist of it is that no the windows 7 is restricted to the list given, but you can attach a task to the sleep/hibernate/shutdown events through the task scheduler. Schedule with the trigger as a log event. For sleep use; Log= System, Source= Kernel-Power, Event ID= 42. And for shutdown System/Security/513.

Andrew Redd
  • 1,722
  • 3
  • 14
  • 13
  • Doesn't seem to work when the laptop power is not plugged in though. – ThreaT Aug 27 '14 at 19:28
  • Doesn't work properly for the Sleep event. It will run after the computer wakes up, not before it goes to sleep. – Andreas Haferburg Sep 08 '17 at 11:23
  • Thanks! For Win10, use System/Kernel-Power/42 for sleep/hibernate and System/Kernel-Power/107 for wakeup. I think shutdown has changed to 4609 according to: https://www.andreafortuna.org/2019/06/12/windows-security-event-logs-my-own-cheatsheet/ – Sujay Phadke Mar 28 '20 at 08:53