批量處理思路在工作中使用的頻率比較高,比如批量清理進程、批量刪除文件、批量機器執行腳本等。
一、批量清理帶java字樣的進程
方式1:使用shell while語法。
ps aux |grep java |awk '{print $2}' | while read line; do kill -9 ${line}; done
shell while 方式直觀,可處理相對復雜的邏輯,通用性高。
方式2:使用 xargs 參數
ps aux |grep java |awk '{print $2}' | xargs kill -9
xargs 方式使用簡潔,可進行簡單邏輯的處理
二、批量刪除文件
刪除當前目錄且不包括子目錄下 '.txt' 文件格式的文件
find ./ -maxdepth 1 -name "*.txt" | while read line; do rm -rf ${line}; done
三、多台機器批量執行命令
查看多台機器的當前時間,機器時間不同步時可以采用這樣方式查看。
方式1:shell for 語法
for host in 10.111.17.119 10.111.17.120; do ssh root@${host} 'date -R'; done
方式2:ansible 命令
ansible hadoop -m shell -a 'date -R' -k
ansible 命令的使用參考:https://www.cnblogs.com/walker-/p/10146660.html