I want to distribute work from the main server to several production servers using packages.
Ideally, I would have a tasks.txt file with a list of tasks to perform
cmd args 1 cmd args 2 cmd args 3 cmd args 4 cmd args 5 cmd args 6 cmd args 7 ... cmd args n
and each working server will connect using ssh, read the file and mark each line as done or done
#cmd args 1 #worker1 - done #cmd args 2 #worker2 - in progress #cmd args 3 #worker3 - in progress #cmd args 4 #worker1 - in progress cmd args 5 cmd args 6 cmd args 7 ... cmd args n
I know how to make an ssh connection, read the file and execute it remotely, but I donβt know how to read and write the atomic operation, so as not to have cases when 2 servers start the same task, and how to update the line.
I would like each employee to go to the task list and block the next available task in the list, and not the server actively commanding the workers, since I will have a flexible number of work clones that I will start or close according to how quickly I need tasks.
UPDATE:
and my idea for a working script would be:
#!/bin/bash taskCmd="" taskLine=0 masterSSH="ssh usr@masterhost " tasksFile="/path/to/tasks.txt" function getTask(){ while [[ $taskCmd == "" ]] do sleep 1; taskCmd_and_taskLine=$($masterSSH "#read_and_lock_next_available_line $tasksFile;") taskCmd=${taskCmd_and_taskLine[0]} taskLine=${taskCmd_and_taskLine[1]} done } function updateTask(){ message=$1 $masterSSH "#update_currentTask $tasksFile $taskLine $message;" } function doTask(){ return $taskCmd; } while [[ 1 -eq 1 ]] do getTask updateTask "in progress" doTask taskErrCode=$? if [[ $taskErrCode -eq 0 ]] then updateTask "done, finished successfully" else updateTask "done, error $taskErrCode" fi taskCmd=""; taskLine=0; done
source share