Batch File For Sleep When Certain Programs Are Running

Discussion in 'Software' started by Joe Ciaravino, Jan 21, 2015.

  1. Joe Ciaravino

    Joe Ciaravino Specialist

    I need a method of retaining my "power savings" settings on Windows 7. But, when PLEX Server is running, I need a way to customize sleep mode so as to prevent my accessing my library from my TV.
     
  2. GermanOne

    GermanOne Guest

    Joe

    That's not enough information to write an already working Batch code. Basically:
    Use TASKLIST and pipe its output to FINDSTR to check if a certain process is running. Depending on the returned errorlevel you may have to do some actions. Using POWERCFG you can change your power settings. (I suggest you to create two different schemes before and toggle with POWERCFG). Use POWERCFG -LIST to get the GUIDs.
    A Batch code could look like that (first three variables have to be customized!)
    Code:
    @echo off &setlocal
    
    set "proc=some.exe"
    set "scheme1=381b4222-f694-41f0-9685-ff5bb260df2e"
    set "scheme2=8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c"
    
    tasklist /nh /fi "imagename eq %proc%" | findstr /bric:"%proc:.=\.%\>" >nul
    if errorlevel 1 (
      REM process is NOT running
      powercfg -setactive %scheme1%
    ) else (
      REM process is running
      powercfg -setactive %scheme2%
    )
    
    All you have to do now is to run this Batch file as a scheduled task. E.g. once an hour or so.

    Regards
    GermanOne
     
  3. Joe Ciaravino

    Joe Ciaravino Specialist

    Thank you. I'm not a programmer, but am willing to try if you give me some more help. Especially don't know how/where to insert any batch file that may be written.

    I am running Windows 7 Home Premium SP 1................64 Bit version
    I have my computer set to "sleep" after 1 hour if there is no activity
    I would like to prevent "sleep" as long as Plex Server is running
    I only run Plex Server if I plan on watching computer videos on my TV

    Regards,
    Joe

    PS: I believe I understand what you're telling me to do, BUT, my computer seems to have only a SINGLE power plan option, so I cannot toggle between the two with a batch file.
     
    Last edited: Jan 25, 2015
  4. Joe Ciaravino

    Joe Ciaravino Specialist

    Look further and now I understand.
    And, yes, I can set multiple power schemes to toggle between.

    Questions:
    Where do I find the registry strings for the 2 power schemes (e.g.: "381b4222-f694-41f0-9685-ff5bb260df2e", and 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c"?
    Is "proc.exe" the name of the new batch file?

    A bit more help on exact syntax of the batch file and I'll be ready to get started.

    THANK you,
    Joe
     
  5. GermanOne

    GermanOne Guest

    Hey Joe

    These are the GUIDs that you'll find if you execute POWERCFG -LIST in a CMD Console. Type CMD into the search box, hit Enter and wait until the CMD Console opens. Now type POWERCFG -LIST and hit Enter. The names and GUIDs of all available schemes will be displayed. Take the GUIDs of your schemes and customize the variables scheme1 and scheme2 in your Batch code.

    No. This has to be the process name that runs for your Plex Server. I don't know what the name is. Open your Task Manager and have a look at the Processes tab. If you figured it out replace proc.exe with the name you found.

    Since you were asking for a Batch file I assumed that you know what it actually is. It's a simple ASCII text file with extension .bat instead of .txt.
     
  6. Joe Ciaravino

    Joe Ciaravino Specialist

    Thanks for giving me much that I need to proceed with.
    Please tell me where/how to insert the batch file?
    Please help me find where in the registry the 2 power schemes that will be toggled are found?
    I will try to muddle through writing the necessary batch file (I don't know how to write such things and am at a total loss as to syntax, spacing, commands, etc etc). I'm asking too much if you can help me with this.

    Danke schön,
    Joe C
     
  7. GermanOne

    GermanOne Guest

    Joe,

    let's do it step by step.

    - First of all - did you already create the two needed power schemes?
    If yes proceed with the next step. If not create them first.
    - Type CMD into the search box, hit Enter and wait until the CMD Console opens. Now type POWERCFG -LIST into the black console window and hit Enter.
    - Please right-click onto the upper bar of the console window and choose "Select All".
    - If the entire text is marked then hit Enter. The content is now copied to the clipboard.
    - Paste the content into the Reply Editor here at MG. (If the text contains your user name or other data you don't want to publish remove it - but leave the scheme names and GUIDs unchanged.)
    - Tell me which of the schemes you want to use.

    After your reply we will go Forward with creating the Batch code ...
     
  8. Joe Ciaravino

    Joe Ciaravino Specialist

    Yes, I did.

    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation. All rights reserved.

    C:\Users\Joe Ciaravino>powercfg -list

    Existing Power Schemes (* Active)
    -----------------------------------
    Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced) *
    Power Scheme GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c (High performance)
    Power Scheme GUID: a1841308-3541-4fab-bc81-f71556f20b4a (Power saver)

    C:\Users\Joe Ciaravino>


    I'd like to use "balanced" as default scheme, and I'd like to use "high performance" as the power scheme while Plex Server is running.
     
  9. GermanOne

    GermanOne Guest

    Great. In the mean time I looked up for the process name in the in internet. If I'm right it's "Plex Media Server.exe" that appears to run. That is, the Plex icon is visible on the right side of your task bar.


    Next steps:
    - create a new folder directly on your C: drive and name it "scheduled"
    - type notepad into the search box, hit Enter and wait until the Notepad Editor opens up.
    - copy the following code into the Editor window:
    Code:
    @echo off &setlocal
    
    set "proc=Plex Media Server.exe"
    set "scheme1=381b4222-f694-41f0-9685-ff5bb260df2e"
    set "scheme2=8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c"
    
    tasklist /nh /fi "imagename eq %proc%" | findstr /bric:"%proc:.=\.%\>" >nul
    if errorlevel 1 (
      REM process is NOT running
      powercfg -setactive %scheme1%
    ) else (
      REM process is running
      powercfg -setactive %scheme2%
    )
    
    Now do the following to save the file:
    - Menu "File"
    - "Save As" (the Save As dialogue box opens)
    - Navigate to "C:\scheduled"
    - make sure you selected the following settings:
    Encoding: ANSI
    Save As Type: All Files [*.*]
    - Write plex.bat into the File Name box
    - Click on "Save"

    Now do some tests if Plex Server is running an if not. Each time double click on plex.bat and observe wether or not the power scheme is changing. (Use the POWERCFG -LIST command again. The asterisk marks the active scheme.)

    Tell me if it works.
    After your reply we will proceed with creating a Scheduled Task.
     
  10. Joe Ciaravino

    Joe Ciaravino Specialist

    YES!!! It works.
     
  11. GermanOne

    GermanOne Guest

    That's good news.

    OK to avoid the manual starting of the batch file we have to schedule it.
    Actually you would need some experiences to create a new Scheduled Tasks. You will find a short description at http://windows.microsoft.com/en-us/windows/schedule-task apart from a lot of advanced tutorials on different web sites.
    Nevermind. Since you need only one new Scheduled Tasks I would rather give you another Batch code to create it automatically instead of wasting your time to guide you through the scheduler wizard.
    Batch code is:
    Code:
    @echo off &setlocal
    cd /d "%~dp0"
    
    for /f "tokens=2 delims=:" %%i in ('chcp') do set /a oemcp=%%~ni
    >nul chcp 1252
    
    set "taskname=plexpowerscheme"
    set "xmlfile=task.xml"
    
    set "line_01=<?xml version="1.0" encoding="UTF-16"?>"
    set "line_02=<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">"
    set "line_03=  <RegistrationInfo>"
    set "line_04=    <Author>GermanOne;MajorGeeks.com</Author>"
    set "line_05=    <Description>observe Plex Server and run batch file to change power scheme</Description>"
    set "line_06=  </RegistrationInfo>"
    set "line_07=  <Triggers>"
    set "line_08=    <LogonTrigger>"
    set "line_09=      <Repetition>"
    set "line_10=        <Interval>PT5M</Interval>"
    set "line_11=        <StopAtDurationEnd>false</StopAtDurationEnd>"
    set "line_12=      </Repetition>"
    set "line_13=      <Enabled>true</Enabled>"
    set "line_14=    </LogonTrigger>"
    set "line_15=    <CalendarTrigger>"
    set "line_16=      <Repetition>"
    set "line_17=        <Interval>PT5M</Interval>"
    set "line_18=        <StopAtDurationEnd>false</StopAtDurationEnd>"
    set "line_19=      </Repetition>"
    set "line_20=      <StartBoundary>2015-02-08T00:00:00</StartBoundary>"
    set "line_21=      <Enabled>true</Enabled>"
    set "line_22=      <ScheduleByDay>"
    set "line_23=        <DaysInterval>1</DaysInterval>"
    set "line_24=      </ScheduleByDay>"
    set "line_25=    </CalendarTrigger>"
    set "line_26=  </Triggers>"
    set "line_27=  <Principals>"
    set "line_28=    <Principal id="Author">"
    set "line_29=      <UserId>S-1-5-18</UserId>"
    set "line_30=      <RunLevel>LeastPrivilege</RunLevel>"
    set "line_31=    </Principal>"
    set "line_32=  </Principals>"
    set "line_33=  <Settings>"
    set "line_34=    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>"
    set "line_35=    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>"
    set "line_36=    <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>"
    set "line_37=    <AllowHardTerminate>true</AllowHardTerminate>"
    set "line_38=    <StartWhenAvailable>true</StartWhenAvailable>"
    set "line_39=    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>"
    set "line_40=    <IdleSettings>"
    set "line_41=      <StopOnIdleEnd>false</StopOnIdleEnd>"
    set "line_42=      <RestartOnIdle>false</RestartOnIdle>"
    set "line_43=    </IdleSettings>"
    set "line_44=    <AllowStartOnDemand>true</AllowStartOnDemand>"
    set "line_45=    <Enabled>true</Enabled>"
    set "line_46=    <Hidden>true</Hidden>"
    set "line_47=    <RunOnlyIfIdle>false</RunOnlyIfIdle>"
    set "line_48=    <WakeToRun>false</WakeToRun>"
    set "line_49=    <ExecutionTimeLimit>P3D</ExecutionTimeLimit>"
    set "line_50=    <Priority>5</Priority>"
    set "line_51=    <RestartOnFailure>"
    set "line_52=      <Interval>PT1M</Interval>"
    set "line_53=      <Count>3</Count>"
    set "line_54=    </RestartOnFailure>"
    set "line_55=  </Settings>"
    set "line_56=  <Actions Context="Author">"
    set "line_57=    <Exec>"
    set "line_58=      <Command>C:\Windows\system32\cmd.exe</Command>"
    set "line_59=      <Arguments>/c "C:\scheduled\plex.bat"</Arguments>"
    set "line_60=    </Exec>"
    set "line_61=  </Actions>"
    set "line_62=</Task>"
    
    <nul >"%xmlfile%" set /p "=ÿþ"
    >>"%xmlfile%" cmd /u /q /c "for /f "tokens=1* delims==" %%i in ('set line_') do echo %%j"
    
    >nul chcp %oemcp%
    
    schtasks /create /xml "%xmlfile%" /tn "%taskname%" /f
    del "%xmlfile%"
    
    pause
    
    To save the file just follow my explanation in #9 but name the file schedtask.bat this time.
    If you are finished to save the file:
    - Right-click onto your "schedtask.bat" and choose "Run As Administrator".
    - Confirm the UAC prompt by clicking on "Yes".
    - The Batch window will open up and displays a message.

    Please tell me what message you got back. It should be either something like "The Scheduled Task was successfully created" or "Access denied".

    After your next reply I'll give you some final informations. (It's already after midnight in Germany. Just in case you're wondering why I don't answer anymore ... ;))
     
  12. Joe Ciaravino

    Joe Ciaravino Specialist

    Please tell me what message you got back. It should be either something like "The Scheduled Task was successfully created" or "Access denied".

    "The following task 'powerplex scheme' has been created.
    Press any key to continue."


    So we now have 2 files in the root drive, in folder "C:\scheduled

    1. plex.bat
    2. schedtask.bat

    And....some investigation tells me that the activation of Plex Server.exe will be checked every five minutes.
    Sheme one has computer sleep after one hour
    Scheme two has computer sleep, never.

    Monitoring every five minutes is OK.

    korrekt?
     
    Last edited: Feb 8, 2015
  13. Joe Ciaravino

    Joe Ciaravino Specialist

    After five minutes, with PLEX Server running , scheme 2 does not come into play?
    Was I supposed to save the second batch file to the folder "C\scheduled, or directly to the root drive?
     
  14. GermanOne

    GermanOne Guest

    You were absolutely correct but you didn't wait for my response ;)

    The message indicates that you succeeded to create the Sheduled Task. That means we are almost finished.

    You don't need "schedtask.bat" anymore. You can delete it if you want. Leave "plex.bat" as it is because the Scheduled Task will run it.

    Just for your information what the task does:
    - There are two triggers to run the task. 1st is if a user logs on, 2nd is the time is 00:00:00.
    - Once the task was triggered it runs your Batch file every 5 minutes one day long.
    - The task will run your Batch file under the SYSTEM account. That means you will never see the Batch window showing up.

    If you want to trigger your task right now you have to restart your computer. Also you should shutdown or restart your computer once a day because the trigger expires after 24 hours.

    If you have any further questions don't hesitate to get back :)
     
  15. Joe Ciaravino

    Joe Ciaravino Specialist

    It works perfectly. You are indeed a wizard! I thank you sehr viel.

    I usually don't shut down my computer and I am the sole user. If possible, is there a way to have the trigger expire after three days instead of one. I almost certainly shut down my computer within every three days. If not, I'll try to remember that I must shut down before using Plex Server.
     
  16. Joe Ciaravino

    Joe Ciaravino Specialist

    Can this be changed to three or four:

    <ScheduleByDay>"
    set "line_23= <DaysInterval>1</DaysInterval>"


    ......or is it more complicated than that?
     
  17. GermanOne

    GermanOne Guest

    That doesn't make any sense. I found that English screenshot (I fear my own German screenshot of the real task wouldn't help you) that shows what the 1 is for:
    http://i.stack.imgur.com/LSUhn.png


    In the Batch code ...
    Code:
    set "line_22=      <ScheduleByDay>"
    set "line_23=        <DaysInterval>1</DaysInterval>"
    set "line_24=      </ScheduleByDay>"
    ... means trigger daily, recur every 1 day.
    I don't think that recur every 3 or 4 days is something that you want to do. Correct?

    To be honest I'm not really sure if the Trigger will expire. I hoped that the 00:00:00 trigger would avoid it. Just try it.
     
    Last edited by a moderator: Feb 9, 2015
  18. Joe Ciaravino

    Joe Ciaravino Specialist

    You are correct, I do not.
    I will try it.
    Thanks again for your generous help.
    Joe Ciaravino
     
  19. GermanOne

    GermanOne Guest

    I also already set StopAtDurationEnd to "false" (Line_11 and Line_18). That was all that came to my mind though. I would be pleased to get a little feedback from you if you know if it works :)
     
  20. Joe Ciaravino

    Joe Ciaravino Specialist

    I will not shut down my computer for a few days, nor logon. I will let you know what happens.
    Joe C
     
  21. Joe Ciaravino

    Joe Ciaravino Specialist

    GermanOne,

    As a followup, I have not shut off nor logged out/in to my computer in three days. I have been testing your work, and it does not seem to require shutdown after one day to reset the first trigger. Apparently, there in no time limit on the first trigger, or, if there is, it won't occur before I usually reboot the thing during the course of the weeks. This is exactly what I wanted.

    It works beautifully! Many thanks, again.
    Joe Ciaravino
     
  22. GermanOne

    GermanOne Guest

    You're welcome! Thank you for your feedback.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds