Run command BEFORE going to sleep?


  1. Posts : 428
    Windows 10
       #1

    Run command BEFORE going to sleep?


    My setup requires me to manually turn my speakers on and off, I want to automate that.

    I got a smart plug, installed python, and the python program that controls the plug

    so I can now run a bat file with the line of
    Code:
    kasa --alias "speakerplug" off
    to turn the speakers off, and "...on" to turn on.

    When the computer starts or I login, and the computer wakes from sleep: I want the command to turn the speakers on to run
    When the computer shuts down and sleeps: i want the command that turns the speakers off to run

    I'm finding solutions for each condition, except when I want the command to run when sleep is initiated. I want the computer to be put to sleep by any normal means and turn off the speakers when doing so,

    There seems to be a program that only works for Windows XP era systems and doesn't work, and there seems to be a Windows 7 era program, but all download links are broken.

    How do I run a command that is automatically triggered when the computer starts to go to sleep, and completes running before the computer goes to sleep?
      My Computer


  2. Posts : 1,767
    Windows 10 Pro (+ Windows 10 Home VMs for testing)
       #2

    FuturDreamz said:
    How do I run a command that is automatically triggered when the computer starts to go to sleep, and completes running before the computer goes to sleep?
    It sounds like you need Task Scheduler to run a command using an event-based trigger.

    Hope this helps...
      My Computer


  3. Posts : 16,950
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #3

    I agree with Rick but I don't think any events occur early enough in the sleep transition to be able to run a script before sleep is achieved.
    The Sleep event I monitor is
    Log - System
    Source - Kernel-Power
    EventID - 42
    Run command BEFORE going to sleep?-sleep-event.png

    [The other one shown in the list is the wake event.]


    To investigate this, you could use NirSoft FullEventLogView to look at any events that might happen before the one I'm monitoring. But I think somebody else looked into this last year and was unable to find any event that could be used as the trigger for their desired pre-sleep script.

    It's possible that this article might identify something you could work with - The Kernel-Power Event Provider - GC



    All the best,
    Denis
      My Computer


  4. Posts : 16,950
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #4

    The OP has also asked this question in Run and complete command when sleep is triggered? - ElevenForum

    I think that both OSes behave in the same way and I do not think there is any EventViewer-based solution for running a script before sleeping.

    Isn't the turn on script after start/restart/wake from sleep/wake from hibernation sufficient?
    Might a combined turn off then turn on script after start/restart/wake from sleep/wake from hibernation be sufficient?


    Denis
      My Computer


  5. Posts : 428
    Windows 10
    Thread Starter
       #5

    Try3 said:
    The OP has also asked this question in Run and complete command when sleep is triggered? - ElevenForum

    I think that both OSes behave in the same way and I do not think there is any EventViewer-based solution for running a script before sleeping.

    Isn't the turn on script after start/restart/wake from sleep/wake from hibernation sufficient?
    Might a combined turn off then turn on script after start/restart/wake from sleep/wake from hibernation be sufficient?


    Denis
    After dealing with Linux for a while i learned to crosspost to every official forum involved with the problem. You don't find solutions unless you ask the support team for the operating system, the team for the upsteam operating system it's based on, the team for the application package in question, and the team for the application library responsible for the specific task needed. Needless to say I'm back on Windows.

    I did looking into just doing a task scheduler, but the command takes about three seconds to run before completing. if it is solely triggered by task scheduler, it will be halted and not complete before the computer goes to sleep, then completes when the computer wakes. that is completely unhelpful.

    I need a way for the command to be able to delay sleep until the command has completed it's execution
      My Computer


  6. Posts : 82
    Win10 pro v1903
       #6

    RickC said:
    It sounds like you need Task Scheduler to run a command using an event-based trigger. Hope this helps...
    I do exactly this to turn on and off my TV with an infrared signal when my PC wakes or sleeps using a USB-UIRT device. The two event based triggers are listed below, each one simply calls a batch file called tv.cmd which tells the USB-UIRT to send the IR burst. I found these events just by trial and error so the batch file could execute quickly enough to finish before the system sleeps, maybe they will work for you.Run command BEFORE going to sleep?-clipboard02.jpg
      My Computer


  7. Posts : 428
    Windows 10
    Thread Starter
       #7

    The other forum had a post that was able to find a download link for the program. I installed version 1.0.1 because when i was looking they said that the version 2 program was glitchy. This is working perfectly.
    Run and complete command when sleep is triggered? | Windows 11 Forum
    das10, post: 327591, member: 379 said:
    RE: "there seems to be a Windows 7 era program, but all download links are broken".
    JFI: "Windows 7 Suspend/Resume Control" is available within the following Codeplex archive, but I have no idea whether it works as desired in Windows 11 or not.

    Internet Archive: View Archive

    Search for this specific project zip on that page: win7suspendresume.zip

    The win7suspendresume.zip project contains the Releases folder with 4 zipped releases, with "49d2ea10-7a2a-4265-81b0-0d8f3eb00d24" being the last one.

    That "49d2ea10-7a2a-4265-81b0-0d8f3eb00d24" contains an setup.exe together with an setup.msi file.
    VirusTotal

    Code:
    "Name": "Version 2.0.0.1",
    "ModifiedDate": "2013-02-05T08:19:15.957-08:00",       ***
    "49d2ea10-7a2a-4265-81b0-0d8f3eb00d24",                ***
    pn: Unfortunately, whilst on my VirtualBox Win11 virtual machine, I could install the software/service, the issues I had were :
    a) There is no way to trigger a proper sleep/suspend/resume in VirtualBox, so couldn't test any further.
    b) All the Program tabs were populated in accordance with what was running in the machine, except the Devices tab which threw the Fault shown (Issue was reported with Windows 10 so presumably the other functions worked in Win10 (?)).

    So experiment with caution on a real Win11 machine.
      My Computer


  8. Posts : 16,950
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #8

    FuturDreamz said:
    After dealing with Linux for a while i learned to crosspost to every official forum involved with the problem.
    I provided cross-references so the users of each forum had a chance to see what was happening in the other one.

    By the way, I think John Hoh's EventID 187 is only generated when using Start button, Power, Sleep & Hibernate. I suspect that it would also get generated by that Rundll32.exe powrprof.dll,SetSuspendState shortcut that some people use
    So that third-party utility das10 pointed you to would seem to remain your best bet.


    Denis
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 12:09.
Find Us




Windows 10 Forums