Start/stop tasks remotely on a cluster of Windows Machines
If you have a cluster of Windows machine but you don’t have a resource manager installed, don’t worry! Windows has it’s tools out of the box to start or stop running tasks from remote.
First of all, you need to have the infrastructure — each are Windows machines with remote desktop connection enabled, physical desktops or a VM on the same LAN:
- master
- worker1
- worker2
- …
- worker8
Then there should be a batch file, a script or an exe you want to run on each workers in the same time. This will be in our example:
\\share\MyTask.bat
Set up the Workers
We will use use schtasks to create tasks on the workers. The syntax of this tool is:
schtasks /Create [/S system [/U username [/P [password]]]] [/RU username [/RP [password]] /SC schedule [/MO modifier] [/D day] [/M months] [/I idletime] /TN taskname /TR taskrun [/ST starttime] [/RI interval] [ {/ET endtime | /DU duration} [/K] [/XML xmlfile] [/V1]] [/SD startdate] [/ED enddate] [/IT] [/Z] [/F]
To create ‘MyTask’ to run ‘\\share\MyTask.bat’ file on the worker is like this:
schtasks /Create /s worker1 /TN MyTask /TR \\share\MyTask.bat
schtasks /Create /s worker2 /TN MyTask /TR \\share\MyTask.bat
schtasks /Create /s worker3 /TN MyTask /TR \\share\MyTask.bat
schtasks /Create /s worker4 /TN MyTask /TR \\share\MyTask.bat
schtasks /Create /s worker5 /TN MyTask /TR \\share\MyTask.bat
schtasks /Create /s worker6 /TN MyTask /TR \\share\MyTask.bat
schtasks /Create /s worker7 /TN MyTask /TR \\share\MyTask.bat
schtasks /Create /s worker8 /TN MyTask /TR \\share\MyTask.bat
Alternatively, you can set up these tasks manually on each worker machines via Task Scheduler.
Important: you need to do it only once!
Set up the Master
We will use schtasks to start tasks on the workers. The syntax of this tool is:
schtasks /Run [/S system [/U username [/P [password]]]] /TN taskname
Create a bat file (e.g. \\share\WorkersStart.bat) to start the Tasks on each workers:
Then whenever you need to run the workers, it’s enough to run the \\share\WorkersStart.bat on your master machine once. You can even set up a scheduled Task in the Task Scheduler to run \\share\WorkersStart.bat automatically.
If you need to stop the tasks, use schtasks to stop the already running tasks on the workers. The syntax is:
schtasks /End [/S system [/U username [/P [password]]]] /TN taskname
Create a bat file (e.g. \\share\WorkersTerminate.bat):
Then similarly to the WorkersStart.bat, you can run it manually or scheduled in Task Scheduler.