1.

Solve : Schedule One Time Task with SchTasks?

Answer»

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,

MJQuote

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.

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,

MJAfter 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.

why not use the runonce registry optionSquashman - RunOnce is executed upon login. I don't want to have to log into the server for the script to execute. Thanks anyway!


Discussion

No Comment Found