https://www.gnu.org/software/parallel/
When I write automation scripts, I try to write them to do "one simple task". For example, it will logon to a single database or a single server and so something.
In reality, I usually want the task to be repeated thousands of times and to be done in parallel to finish quickly.
Alternatively, I some times write the script to only print out the statement required to complete the task on the command line.
With GNU parallel I can use it to control my script and have it run in parallel and throttle the process. GNU parallel can limit the script to a certain number of instances of the script running at the same time.
In this example, my "createSomething.py" script only print out the commands to do the work. It will print out hundreds of commands which need to be run.
By piping it into parallel, it will automatically run 10 of those processes at a time and keep running them until they are all done.
./createSomething.py <input values> | parallel
In this example the file server_list.txt has multiple columns. Each column in the text file is separated by a tab. This is specified in --colsep '\t' as seen below. The values from each line will appear where is {}.
cat server_list.txt | parallel -j10 --colsep '\t' "my_shell_script.sh {}"
In this example the value which is read from server_list.txt is put into the input values after "my_python_script.py" where {.} is:
cat server_list.txt | parallel -j10 python my_python_script.py {}
Here is another example running a query on a long list of servers:
contents of long_list_of_servers.txt would look like:
server1.com
server2.com
server3.com
Execute whatever code is in the "execute_this.sql" on every server in long_list_of_servers.txt. I use the -vvv option to log all the commands so they can be reviewed.
cat long_list_of_servers.txt | parallel -j10 "mysql --defaults-file='my_defaults_file' -h '{}' -vvv < execute_this.sql >> outputfile.log"
If the code in "execute_this.sql" was a grant statement to create a new user or add more permissions on a list of Aurora instances, you could verify that it worked by running this next:
cat long_list_of_servers.txt | parallel -j10 "mysql --defaults-file='my_defaults_file.cnf' -h '{}' -e \"show global variables like 'aurora_server_id'; show grants for myUSER; '\" >> outputfile.log"
Now the log file will have the aurora_server_id for each servers and you can verify that each instance has the correct grants.
No comments:
Post a Comment