Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I know there is the at and schtasks. But I couldn't figure out how to use them to execute a task soon and once. I have read about a soon command, but there isn't such command in my Windows version.

I plan to include this command in a batch file and, because of that, I can't hardcode the start time.

share|improve this question
add comment

5 Answers

up vote 5 down vote accepted

The schtasks command can run a defined task immediately.

From How to use Schtasks.exe to Schedule Tasks in Windows Server 2003

Schtasks /Run

Starts a scheduled task immediately. The run operation ignores the schedule, but uses the program file location, user account, and password that are saved in the task to run the task immediately.

Syntax:

schtasks /run /tn TaskName [/s computer [/u [domain\]user /p password]] /?

So just define the task, then run it immediately at any time you need to.

share|improve this answer
    
I just need to know now how to create a scheduled task without a schedule, to use your solution. Otherwise I could risk running it twice. –  Jader Dias Feb 13 '11 at 16:14
    
You could define a schedule that can never be executed. You might also try /Run of a disabled job - it might work (the documentation doesn't say no). –  harrymc Feb 13 '11 at 16:30
add comment

Working on top of Keith's answer to suit your needs:

timeout /t X ; psexec -i -s Y

Where

  1. timeout replaces sleep in Windows 7.
  2. X is the seconds to wait defined by /t, add /nobreak if you want to disallow the user to continue.
  3. psexec from Microsoft SysInternals allows you to run interactive (-i) as the local system (-s), please note that you have to start psexec once to accept the first time EULA, or add -accepteula.
  4. Y is your command.

runas doesn't allow you to start as local system, that's why to use PsTools for process-related tasks.
Starting a Scheduled Task or Service seems like too much of a workaround for me...


For example, Patrick's answer can simply be rewritten as:

timeout /t 120 ; psexec -i -s calc.exe
share|improve this answer
add comment

This starts calc.exe in 2 minutes. Change the

set /A START_MIN=%%j + 2

line to change the time interval.

for /f "delims=:. tokens=1,2" %%i in ("%time%") do (
set /A START_HOUR=%%i
set /A START_MIN=%%j + 2
)

IF %START_MIN% GEQ 60 (
call :AdjustStartTime
)

IF %START_MIN% LSS 10 SET START_MIN=0%START_MIN%

SCHTASKS /Create /SC ONCE /TN "My Scheduled Task Name" /RU "" /TR "C:\Windows\System32\Calc.exe" /ST %START_HOUR%:%START_MIN%

:AdjustStartTime
SET /A START_HOUR=%START_HOUR% + 1
SET /A START_MIN=%START_MIN% - 60
GOTO:EOF
share|improve this answer
add comment

If you don't want to use at, a quick way is to just sleep before your main program.

sleep 10 ; mycommand

Or put that in a script.

share|improve this answer
    
One of my goals is to execute the command as the LocalSystem user. Your solution doesn't suit my needs. –  Jader Dias Feb 8 '11 at 22:16
    
@Jader: That's important information you need to state in the question. –  Ben Voigt Feb 19 '11 at 19:24
add comment

Patrick S.'s answer met my needs.

I would like to mention however that the quotation marks do not need to be there following the /TR and for whatever reason, seemed to break my script.

Upon removal, his script worked great!

My usage was in an Outlook rule. A script is called and an email is written to the drive with a plugin (messagesave). This is to post to a website from my office PC for a website and my original script that calls the batch needs to terminate prior to a file being written. I had a tough time figuring out how to appropriately delay the scheduled task, even after hitting the 'schtasks' TechNet page.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.