Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Schedule One Time Task with SchTasks  (Read 27568 times)

0 Members and 1 Guest are viewing this topic.

powlaz

    Topic Starter


    Beginner
  • Thanked: 1
    Schedule One Time Task with SchTasks
    « on: May 01, 2013, 06:52:49 PM »
    I need to be able to schedule a task from a batch file that I run using SchTasks.  BUT . . . heheh . . . I need the task to run on startup - once, and then delete itself.

    The task will be set to run on Server 2003 and Server 2008R2 machines.  The command line switch for a task to delete itself doesn't work on "startup" tasks because they run at every startup, not just one time.  What I think I need is a "run once" task that is set to run 10 minutes after the task is created.

    Here's how it would work:

    Script does a bunch of things
    Script creates task for 10 minutes from now
    Server is rebooted
    Server comes back online
    Task executes

    Can it be done?  How do I do it?  I've experimented a little with trying to set a variable equal to time +10, thinking that I could specify the variable in the schtasks time parameter but I can't figure out how to add 10 to "time" . . . Any ideas?


    Thanks for the help,

    MJ

    DaveLembke



      Sage
    • Thanked: 662
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Schedule One Time Task with SchTasks
    « Reply #1 on: May 01, 2013, 06:59:35 PM »
    Quote
    Here's how it would work:

    Script does a bunch of things
    Script creates task for 10 minutes from now
    Server is rebooted
    Server comes back online
    Task executes

    This can be done, through a series of batches all calling and removing each other from startup, and here is how I have done it before.

    Start batch#1 which runs stuff at the get go and then has a SLEEP timer which gives you your 10 minute delay you need before the next event triggers. This batch completed its sleep delay and then copies Batch#2 to startup folder of system, system reboot is passed, and system reboots and now automatically triggers Batch#2 as part of startup.  Batch #2 now calls for Batch # 3 which runs the final chunk of whatever needs to be done and then batch # 3 performs a final cleanup that deletes Batch # 2 from startup. Startup is now empty of any batches and process is complete.


    powlaz

      Topic Starter


      Beginner
    • Thanked: 1
      Re: Schedule One Time Task with SchTasks
      « Reply #2 on: May 01, 2013, 07:34:01 PM »
      Dave - thanks for the fast reply.  I may have misunderstood what you wrote but the startup I was referring to was system start.  The scheduled task should execute on system start and then delete itself.  I don't intend to log into the servers.

      So since scheduling a task to run at system start precludes me from being able to have the task delete itself I think I need to schedule a task to run once (because these types of tasks can delete themselves) but at a time that is 10 minutes from when the task is created. 

      Is it possible to do this?  Is it possible to query the time add 10 minutes and set the new time equal to a variable that can be passed to the schtasks command?

      Thanks,

      MJ

      powlaz

        Topic Starter


        Beginner
      • Thanked: 1
        Re: Schedule One Time Task with SchTasks
        « Reply #3 on: May 02, 2013, 06:59:58 AM »
        After a bit of searching I found this:

        Code: [Select]
        @echo on
        for /F "tokens=1-3 delims=:." %%a in ("%time%") do (
           set Hour=%%a
           set Minute=%%b
           set Seconds=%%c
        )
        ::Convert HH:MM to minutes + 10
        set /A newTime=Hour*60 + Minute + 10
        rem Convert new time back to HH:MM
        set /A Hour=newTime/60, Minute=newTime%%60

        ::rem Adjust new hour and minute
        if %Hour% gtr 23 (set Hour=0) ELSE (IF %Hour% lss 10 set Hour=0%Hour%)
        if %Minute% lss 10 set Minute=0%Minute%
        Set TaskTime=%Hour%:%Minute%:%Seconds%
        Echo %TaskTime%

        I will use the TaskTime variable to schedule the task like this:

        Code: [Select]
        Schtasks /create /ru "System" /s localhost /tn "My Task" /tr "<path to my script>" /sc once /st %TaskTime% /F /V1 /Z
        Note, the /V1 switch in the the Schtasks command is not needed for Server 2003.


        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other
        Re: Schedule One Time Task with SchTasks
        « Reply #4 on: May 02, 2013, 02:50:57 PM »
        why not use the runonce registry option

        powlaz

          Topic Starter


          Beginner
        • Thanked: 1
          Re: Schedule One Time Task with SchTasks
          « Reply #5 on: May 20, 2013, 07:39:48 AM »
          Squashman - RunOnce is executed upon login.  I don't want to have to log into the server for the script to execute.  Thanks anyway!