{{tag>Brouillon Shell CA}}
= Notes commande xargs
Strip char / trim => utiliser ''xargs'' ex :
load5=$(echo $UPTIME | awk -F, '{print $5}' | xargs)
md5sum multithread
#find . -not -name "md5sum.txt" -follow -type f -exec md5sum {} \; > md5sum.txt
find . -not -name "md5sum.txt" -follow -type f -print0 |xargs -0 -L1 -P"$(nproc)" md5sum > md5sum.txt
dig A +short acme.local |xargs -P5 -I '{}' bash -c 'ping -c 1 -W 2 {} >/dev/null 2>&1 || echo {}'
cat 20_ok_ping.txt |xargs -L1 -I '{}' -P50 bash -c 'SRV={} ; ssh -p 22 -o PasswordAuthentication=no -o ChallengeResponseAuthentication=no -o PreferredAuthentications=publickey -o StrictHostKeyChecking=no -o ConnectTimeout=3 root@${SRV} true 2>/dev/null && echo +${SRV} || echo -${SRV}' |tee 30_ssh_p22.txt
pidof sleep | xargs kill -9
Renommer tous les fichiers *.txt d'un répertoire
ls -1 ${repertoire}/*.txt | xargs -I {} -t mv {} {}.csv
cat bash-xargs-demo.txt | xargs -I % sh -c 'echo %; mkdir %'
xargs -a bash-xargs-demo.txt -I % sh -c 'echo %; mkdir %'
Prompt before launch
find * | xargs -p rm -rf
use xargs to copy a file to multiple directories at once; in this example we are trying to copy the file.
echo ./Templates/ ./Documents/ | xargs -n 1 cp -v ./Downloads/SIC_Template.xlsx
Avec délimitateur
$ echo 'my new file' | xargs -d '\n' touch
$ ls -l
total 0
-rw-r--r--. 1 shs shs 0 Oct 15 12:41 file1
-rw-r--r--. 1 shs shs 0 Oct 15 12:41 file2
-rw-r--r--. 1 shs shs 0 Oct 15 12:41 file3
-rw-r--r--. 1 shs shs 0 Oct 15 12:57 'my new file'
$ echo "1,2,3,4,5" | xargs -d, echo
1 2 3 4 5
$ echo "1,2,3,4,5" | xargs -d, -L 1 echo
1
2
3
4
5
Copie de fichiers
find /backup/ -type f | xargs -n1 -i cp {} /var/www/backup/
Curl - flood
seq 1 200 | xargs -n1 -P10 curl "http://localhost:5000/example"
Curl - test urls
xargs -a urls.txt -L1 -I '{}' -P50 bash -c 'echo $( curl --connect-timeout 2 -k -s -o /dev/null -w "%{http_code};" "{}" ; echo "{}" )' | tee ping_urls.csv
Curl can now fetch several websites in parallel
curl --parallel --parallel-immediate --parallel-max 3 --config websites.txt
''websites.txt''
url = "website1.com"
url = "website2.com"
url = "website3.com"
=== xargs avec Ansible
Voir aussi :
* [[Ansible - Accélérer Ansible grâce au module Mitogen]]
* traitements_paralleles [[notes_ansible]]
xargs -a inventory.lst -L1 -I % -P50 bash -c "ansible --ssh-common-args='-o StrictHostKeyChecking=no' -i %, -e ansible_port=2222 -b -u admin -m command -a 'grep -q 192\.168\.223\.0 /etc/hosts.allow' all" |tee outpout.log
4 GB RAM is recommended per 100 forks... about 100MB per fork... The baseline value for [CPU capacity for forks] is 4 forks per core.
Ref : https://docs.ansible.com/ansible-tower/3.4.3/html/userguide/jobs.html#at-capacity-determination-and-job-impact