29

In my windows, I want to schedule a windows service to start once every 10 seconds. I tried using the windows task scheduler but it only gives me an option to repeat the service daily, weekly and monthly.

Is there a way I can schedule the windows service to start once every 10 seconds using windows task scheduler?

What could be done?

6
  • 1
    After the service is started you can just execute the operation you want every 10 seconds from inside the service.
    – user77188
    Jun 6, 2011 at 8:18
  • What are you trying to execute every 10 seconds please?
    – KCotreau
    Jun 6, 2011 at 11:17
  • +1 Is that possible?
    – alex
    Nov 4, 2012 at 0:13
  • 1
    What does it matter what you are trying to execute? How about a simple http get? Why does windows SUCK SO MUCH
    – niken
    Aug 25, 2016 at 19:10
  • Just an addition (does not solve the "10 seconds problem", but fits here for similar problems): In this article François-Xavier Cat describes that the selection boxes (5 minutes, 10 minutes, etc.) are editable. So you can simply click and write "7 Minutes" into them and you will have a task that is executed every 7 minutes.
    – Grimm
    May 13, 2020 at 13:22

4 Answers 4

5

To do that, you should write a windows service, as that is what they are for.

48

A Windows Task Scheduler trigger cannot repeat more often than every 1 minute, but you can set up multiple triggers. To run a task every 10 seconds, add six Triggers. Each one should run the task Daily, and Repeat task every 1 minute. Their start times should be 12:00:00 AM, 12:00:10 AM, 12:00:20 AM, 12:00:30 AM, 12:00:40 AM, and 12:00:50 AM.

Edit Trigger dialog

Silly, but it works.

5
  • 1
    I can confirm that this works, thanks for sharing this. Easiest solution I have seen.
    – Abela
    Jan 11, 2016 at 1:05
  • 4
    See this Stack Overflow Answer for a convenient way to create many triggers for one task by exporting it to a text file.
    – Andre
    Jan 13, 2016 at 15:31
  • 3
    Convenient? I think you mean manageable ;)
    – niken
    Aug 25, 2016 at 19:11
  • @niken you are replying to a commenter called andre, you should use the "@" symbol, so it's clear you are referring to his comment(Which you are, he used the word "convenient",. and to make it clear that you are not referring to the answer.
    – barlop
    Jun 29 at 4:42
  • This is also relevant lazywinadmin.com/2012/03/run-this-task-every-minute.html for example it doesn't show 1 minute, you have to type it in!
    – barlop
    Jun 29 at 5:10
5

It's silly windows doesn't have this functionality built into Task Scheduler. However, it can be easily worked around with a simple powershell script.

 $i = 0
 for ($i=0; $i -le 4) 
   Start-Service -Name "servicename"  
   sleep 10
   $i++
 }

Save this as a *.ps1 file on your host. Then follow Adam C's task scheduler settings and schedule this to run every minute. This will start the service (which I named "servicename") every 10 seconds.

1
  • well, linux's cron can't do second as a unit.. also, the problem with the task scheduler solution is it is set to a particular time. so if the time is 6:30am and in task scheduler you set it to 12:00am then it won't run. Or if you turn the computer on at 1am then it won't run
    – barlop
    Jun 29 at 6:42
0

You can use autohotkey

The following autohotkey solution is much better than the windows task scheduler solutions because the task scheduler solutions only work from a specific time. So if your computer were turned on after that time then you'd have to wait for the clock to reach that time. This just operates as soon as the script starts.

With autohotkey, you can use either a)a timer or b)a while true and a sleep. (Note- i've tested both.. both seem fine.. and btw the while loop with the sleep doesn't hog cpu)

Suppose your autohotkey file is blah.ahk

And the thing you want to run is a bat file.

while true
{
Run cmd /c c:\blah\blah.bat,,Hide
sleep 10000  ; 10 seconds
}

The bat file could have a list of commands. Or you could run an exe Run c:\blah\blah.exe

You can have a main autohotkeys file with include lines , one for each of the ahk scripts that you like to run. That avoids having lots of tray icons, or having one giant ahk file.

A timer version looks like this

setTimer, label, 10000     ;10 seconds
return

label:
; do some commands
return

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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