|
Answer» Hello, I have a doubt in Windows batch programming,
Straight to the point, how to run a program at regular interval?
I have a batch script[Check.bat] which checks for a process in the running list, if it is running then the program quits, else the program shuts down the system.
Now I need another script which launches 'Check.bat' at every 1 minute.
Can anyone help me please!Task schedulerI tend to hard code this into the program, so instead of STARTING and stoping, it just performs it's task every minute instead of exiting. This allows you to drop it in the background and not have to worry about a little black window popping up all the time.
Code: [Select]@echo off REM this sets %m% equal to the minute of the current time. set m=%time:~3,2% :loop1 REM this will add 1 to the minute, telling it when it should stop set /a m+=1 REM this stops it from waiting for 1:89:00 AM :D if %m% EQU 60 set m=0 :loop2 REM this waits for the minute to equal whatever %m% is equal too, in this case, it is the minutes +1 from the last time you passed loop1 if %m% EQU %time:~3,2% goto yourProgramCode REM this sends you back to loop2, making sure that you can only exit the loop when the time is correct goto loop2
:yourProgramCode ... ... ... REM this goes back to loop1 to add 1 to %m% and to wait a minute goto loop1 As long as your program doesn't take over a minute to complete, then this should WORK. If it does, then you'll have to change the code a bit to refresh at loop1 to =%time:~3,2% instead of +=1. Hope this helped.
EDIT: Fell free to remove the lines that start with "REM" if it hurts your eyes.This is dependent on on your regional and language settings. Not everyone displays the time and date the same way. You could just use the PING trick to delay execution and then rerun the loop or if you are on Vista or Windows 7 you can use the TIMEOUT command.That is very interesting, never knew that. I'm sure you can alter the :~#,# thing to get it correct for your system if you were so inclined. I like the timeout command, that will come in handy later.Task scheduler could be set to invoke cmd /c START /MIN batchfile.bat. the batch file will appear as a minimized window at each invocation.
adding this sort of FUNCTIONALITY is called "re-inventing the wheel" and unless you want to learn more about the wheel (or in this case, how to invoke programs at an interval yourself) it is a sub-par solution. Linux has crontab for this, and Windows has task scheduler for this. There is no reason to not use these for scheduled tasks.Quote from: BC_Programmer on June 08, 2012, 05:21:14 AM Task scheduler could be set to invoke cmd /c START /MIN batchfile.bat. the batch file will appear as a minimized window at each invocation.
adding this sort of functionality is called "re-inventing the wheel" and unless you want to learn more about the wheel (or in this case, how to invoke programs at an interval yourself) it is a sub-par solution. Linux has crontab for this, and Windows has task scheduler for this. There is no reason to not use these for scheduled tasks.
I would argue that using Task Scheduler could be considered "reinventing the wheel" if you had never used it before and opted for hard-code instead. I personally feel that hard-coding in the interval helps develop your skills more as well as giving you more system molecularity.Code: [Select]schtasks /create /tn "repeat app.exe every minute" /tr "app.exe" /sc MINUTE Would do precisely what is required, if set to run the OP's batch file. Quote from: BC_Programmer on June 08, 2012, 06:13:08 PMCode: [Select]schtasks /create /tn "repeat app.exe every minute" /tr "app.exe" /sc MINUTE Would do precisely what is required, if set to run the OP's batch file.
Cool, how would you include this into a program without it making a new task upon every start-up/run? Unless the user has to type this in when first installing the program, you would need two different .bat files, one to "install" the program, and the program. No offence, but this seems much more confusing for people who don't program. This method LOOKS GREAT if you are the only one who plans to use the program you are using. But if you plan on distributing the program, this method seems detrimental.
Overall I feel like this would be a great choice if you planned on running the program only on your computer, but the hard-code method looks easier if say you had 300 computers on a network that you needed to format for a company.Quote from: Lemonilla on June 10, 2012, 08:27:20 AMCool, how would you include this into a program without it making a new task upon every start-up/run?
By adding the /F switch.Quote from: BC_Programmer on June 10, 2012, 11:08:59 AMBy adding the /F switch.
Alright you win That probably is a better idea.
|