30

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?

Parth Bhatt
  • 427
  • 1
  • 6
  • 9
  • 1
    After the service is started you can just execute the operation you want every 10 seconds from inside the service. –  Jun 06 '11 at 08:18
  • What are you trying to execute every 10 seconds please? – KCotreau Jun 06 '11 at 11:17
  • +1 Is that possible? – alex Nov 04 '12 at 00: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 '16 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 '20 at 13:22
  • Please change the accepted answer on this one, if you would. – Tripp Kinetics Jun 10 '20 at 02:36

4 Answers4

50

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.

Adam C
  • 868
  • 8
  • 8
  • 1
    I can confirm that this works, thanks for sharing this. Easiest solution I have seen. – Abela Jan 11 '16 at 01:05
  • 4
    See this [Stack Overflow Answer](http://stackoverflow.com/a/13740720/1078146) for a convenient way to create many triggers for one task by exporting it to a text file. – Andre Jan 13 '16 at 15:31
  • 4
    Convenient? I think you mean manageable ;) – niken Aug 25 '16 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 '22 at 04:42
  • This is also relevant https://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 '22 at 05: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.

G_Style
  • 150
  • 1
  • 5
  • 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 '22 at 06:42
5

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

soandos
  • 24,206
  • 28
  • 102
  • 134
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
barlop
  • 23,380
  • 43
  • 145
  • 225