Solved Execute BAT script containing infinite loop automatically at boot

Joe7

Member
Member
Local time
1:14 PM
Messages
58
I'm really struggling to get a BAT script with an infinite loop to run in background on system startup.
Here's the script:
Code:
@ echo off 
setlocal enabledelayedexpansion
set log=c:\testscript.log
echo %date% %time%     Script started > %log%
for /l %%n in (0,0,1) do (
  echo !date! !time!    Check done... >> %log%
  timeout 10 > nul
)

I tried:
- running from command line prompt, and it works fine
- windows task scheduler
- sc tool to add it as a service

But as a service it doesn't start.
With the scheduler it starts, writes "Script Started", enters the loop one time and writes "Check done...", but then it doesn't write anything else.
Maybe the scheduler runs it and then stops it immediately?

So, how can I make this work?
Thanks in advance! :)
 

My Computer My Computer

At a glance

Windows 7 Home Premium 64bit
Computer type
PC/Desktop
OS
Windows 7 Home Premium 64bit
Bat files aren't typically used to make infinite loops and sometimes this can be a problem if a bat file runs for too long. Probably the best solution would be to change the file to simply do one iteration and exit, and setup Task Scheduler to run it periodically forever (effectively building the infinite loop within TS). This is more similar to what both tools are meant to be used.

I have no idea really why TS fails immediately after the first iteration, you probably have to dig the logs in event viewer to find out.
A service is a special kind of executable, that calls a specific set of Windows APIs in a particular sequence to interface with the operating system, you simply can't make one in a bat file.
 

My Computer My Computer

At a glance

Windows 7 Ultimate x64Intel Core i7-740QM8 GB DDR3NVIDIA GeForce 330GT
Computer type
Laptop
Computer Manufacturer/Model Number
Toshiba Sattelite A665-S6092
OS
Windows 7 Ultimate x64
CPU
Intel Core i7-740QM
Memory
8 GB DDR3
Graphics Card(s)
NVIDIA GeForce 330GT
Screen Resolution
1366x768
Hard Drives
Samsung 840 SSD 500GB
1TB USB3 external HD
Cooling
Coolermaster Notepal U3 notebook cooling pad
Internet Speed
3mbps ASDL
Antivirus
ClamWin 0.98.7
Browser
Opera 12.17 x86 (main), Firefox 38 (sec), IE11 (last resort)
I cannot reply, maybe my post contains some unwanted text, or there is some sevenforum issue.
Anyway... I pasted here my reply:

https://paste.c-net.org/RealistSauna

Thanks again!
 

My Computer My Computer

At a glance

Windows 7 Home Premium 64bit
Computer type
PC/Desktop
OS
Windows 7 Home Premium 64bit
Back
Top