Windows Run Scheduled Task After Wake Up with Command Line

08
2014-06
  • spryno724

    I am trying to use the command line to create a scheduled task that will run after I log into my account, whether it be for the first time on that day, or after resuming from sleep/hibernate and then logging in.

    I found the schtasks command, and this looks very much like what I need to run a task after logon: http://technet.microsoft.com/en-us/library/cc772785%28v=ws.10%29.aspx#BKMK_logon

    schtasks /create /tn TaskName /tr TaskRun /sc onlogon
    

    After running my command, I can see my task name listed in the scheduled tasks list, but it never runs, regardless of whether I logon from a refresh restart or after resuming from sleep/hibernate. Here is my command:

    schtasks /create /tn "AutohotKey Windows Logon Trigger" /tr "calc" /sc ONLOGON
    

    Any ideas?

    Thank you for your time.

  • Answers
  • Brian

    For complex tasks it is easier to create the task in the GUI, export it as XML and then on the command line use SCHTASKS /CREATE /tn TaskName /XML xmlfile.

    The event triggers in the GUI you likely want are listed as "At log on" and "On workstation unlock".


  • Related Question

    Windows 7 scheduled task returns 0x2
  • demmith

    I have identical scheduled tasks running in Windows XP Pro and Windows 7. The XP Pro one runs fine, the Windows 7 one always returns 0x2 (which means, "The system cannot find the file specified"; however, executing from the command line is no problem) in the Last Run Result column of the Task Scheduler UI.

    The scheduled task executes a .bat file daily. The .bat file contains a call to execute a Perl script. As I stated in the previous paragraph, it executes under XP without any trouble but under Windows 7, no dice.

    The task under Windows 7 is set to "run whether the user is logged on or not." In this case it is me, I am the only user of the system. It is also set to "Run with highest privileges." And it is not hidden. The .bat file executes perfectly well from the command line - it calls the Perl script as expected and the Perl script does its thing.

    I have searched far and wide looking for an appropriate answer to this issue. So far I have found nothing. What the devil is going on with this Win7 scheduled task? I am ready to pull my hair out.


  • Related Answers
  • KERR

    I had this issue as well. 0x2 from task scheduler seemed to say it couldn't find the batch file. This was false - I added some pipe arrows (>>) to output my batch file to a logfile, then ran the scheduled task.

    Despite getting a 0x2 in Task Scheduler, I found the batch file had actually run, but had encountered an error (which showed up in the logfile).

    Another thing to try is running the task as SYSTEM or NETWORK SERVICE instead.

  • soandos

    I was trying to sync a folder pair with SyncToy 2.1 across two Windows 7 SP1 workstations. Initially, I just browsed for the file and added the arguments for the task. It returned a code of 0x2. Microsoft TechNet recommends using the "Start In" field to ensure the file name is resolved to the correct location. I copied the SyncToy directory path with no quotes or a trailing slash into the "Start In" box, saved it and ran the task. The result was success, the code 0x0!

  • Chris

    I just had this same issue and resolved it. In the Actions tab, under Program/Script: just put the name of your batch file. In the "Start In (optional):" section, put the path to your file with no quotes and no ending slash.

    Example below for running a test.bat file out of a test folder on the root of C:

    Program/Script: test.bat

    Start In (optional): C:\Test