J'aimerais réécrire cette ligne de code de façon à ce que le | head arrête le process avant le pipe dès qu'il reçoit la première ligne.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
$ time for file in $(command ls -1t /var/log/dovecot.log*); do zgrep --line-buffered -E 'login.*xxx' $file | tail -1; done
Oct 11 09:58:19 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=192.168.211.106, lip=10.10.10.19, lport=993, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Oct 11 03:33:19 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=105.101.229.68, lip=10.10.10.19, lport=143, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Oct 09 18:08:34 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=192.168.211.106, lip=10.10.10.19, lport=993, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Oct 06 15:40:23 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=192.168.211.106, lip=10.10.10.19, lport=993, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Oct 05 15:33:26 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=192.168.211.106, lip=10.10.10.19, lport=993, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Oct 04 18:26:25 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=105.235.131.14, lip=10.10.10.19, lport=143, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Oct 03 10:47:36 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=192.168.211.106, lip=10.10.10.19, lport=993, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Oct 02 15:25:28 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=192.168.211.106, lip=10.10.10.19, lport=993, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Sep 30 22:54:10 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=105.235.132.20, lip=10.10.10.19, lport=143, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Sep 29 15:06:41 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=192.168.211.105, lip=10.10.10.19, lport=993, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Sep 28 08:27:34 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=192.168.211.105, lip=10.10.10.19, lport=993, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Sep 27 09:40:33 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=192.168.211.105, lip=10.10.10.19, lport=993, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Sep 26 15:29:57 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=192.168.211.105, lip=10.10.10.19, lport=993, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Sep 25 16:28:54 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=192.168.211.105, lip=10.10.10.19, lport=993, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Sep 22 21:02:17 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=105.235.133.148, lip=10.10.10.19, lport=143, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Sep 21 13:34:07 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=192.168.211.105, lip=10.10.10.19, lport=993, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Sep 20 17:23:33 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=192.168.211.105, lip=10.10.10.19, lport=993, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Sep 19 12:06:51 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=192.168.211.105, lip=10.10.10.19, lport=993, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Sep 18 12:21:34 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=192.168.211.105, lip=10.10.10.19, lport=993, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Sep 15 15:18:47 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=10.10.10.19, lip=10.10.10.19, lport=143, service=imap
Sep 14 17:11:55 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=192.168.211.95, lip=10.10.10.19, lport=993, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Sep 13 15:45:08 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=192.168.211.95, lip=10.10.10.19, lport=993, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Sep 12 13:45:40 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=192.168.211.95, lip=10.10.10.19, lport=993, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Sep 11 17:53:37 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=192.168.211.95, lip=10.10.10.19, lport=993, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
 
real    0m5.367s
user    0m6.004s
sys     0m0.356s
$
Comme on le vois, la commande prend environ 6 secondes à s'executer lorsqu'il n'y a pas de head.
Je m'attend à ce que ça prend beaucoup moins de temps lorsqu'il y en a un, comme ici :


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
 
$ time for file in $(command ls -1t /var/log/dovecot.log*); do zgrep --line-buffered -E 'login.*a.chaouche' $file | tail -1; done | head -1
Oct 11 09:58:19 imap-login: Info: Login: user=<xxx@mydomain.tld>, method=PLAIN, rip=192.168.211.106, lip=10.10.10.19, lport=993, service=imap, ssl=TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
 
real    0m4.681s
user    0m5.088s
sys     0m0.424s
$
Au lieu de ça, la commande prend tout de même 5 secondes à s'executer, ce n'est pas énorme en gain de temps.
Je vois bien la première ligne s'afficher et head qui attend encore, longtemps après la première ligne.

Une idée ?

PS : la ligne de code a pour objet de mettre en évidence le dernier login d'un utilisateur au serveur dovecot.
Pour ce faire, j'ai imaginé une boucle for qui va scanner tous les fichiers de log dovecot dans l'ordre chronologique inverse (plus récent vers plus ancien)
Je fais un grep sur chaque fichier et j'en prend la dernière ligne.
De ce fait, si, dans la même journée, une personne se connecte à 09h00 puis à 17h00, c'est 17h00 qui sera affichée.
Le prochain tour de boucle affichera le dernier login (s'il y en a) de la journée précédente.
Le | head à la fin devrait donc afficher le tout dernier login.
S'il y a eu connexion lundi, les fichiers de dimanche, samedi, vendredi... ne devraient pas être scannés.
Mais j'ai l'impression qu'ils sont tout de même scannés vu le temps que ça prend.