In MS Windows, you can use the Task scheduler to run a task at startup for an interval of time forever. In the example below, I put the following codes in the echo-date-time.bat batch file:
echo %date% %time% >> c:\xuan\schedule-task-output.txt
The script will write the current date and time to c:\xuan\schedule-task-output.txt. Here are the steps to create a task in the Task scheduler to run the script at startup and for every hour.
- Open the Task Scheduler.
- Create a task.
- On the General tab,
- Enter the task name.
- Select Run whether user is logged on or not. You will need the Administrator password.
- Select Run with highest privileges in case your task requires Administrator privilege to run.
- On the Trigger tab, set to begin the task at startup and repeat the task every 1 hour and indefinitely.
- On the Action tab, set your program/script. If your program requires input parameters, then add them in Add arguments (optional) input field. For my case, it is echo-date-time.bat.
If you set your Trigger to begin the task At startup, then that task will NOT run until you restart your computer. It doesn't matter if you manually force the task to run now, it will not run. Even though the Status is set to Running, it doesn't run because Next Run Time is empty.
For the case where you don't know when your server is going to be restarted, create a duplicate task but set the trigger to begin the task On a schedule for One time only.