https://www.suse.com/news/SUSE-Preserves-Choice-in-Enterprise-Linux/
Via
Posted by jpluimers on 2023/07/11
Posted in Uncategorized | Leave a Comment »
Posted by jpluimers on 2023/07/11
pidof python | tr " " "\n" | xargs -r -n 1 lsof -i -a -e /run/user/1001/gvfs -p
Breakdown:
pgrep
installed):# pidof python 26128 12583
xargs
prefers line separated, lets replace spaces with newlines (I showed this before in Source: firewalld: show interfaces with their zone details and show zones in use):# pidof python | tr " " "\n" 26128 12583
xargs
squashes all input on one line:# pidof python | tr " " "\n" | xargs echo 26128 12583
-L 1
or -n 1
argument to keep them on separate lines:# pidof python | tr " " "\n" | xargs -L 1 echo 26128 12583 # pidof python | tr " " "\n" | xargs -n 1 echo 26128 12583
lsof
can not only show open files, but also IP sockets (-i
), and *only* those (-a
), for a specific process ID (-p #
). So by having the -p
as last argument, xargs
will append the process ID after it:# pidof python | tr " " "\n" | xargs -n 1 lsof -i -a -p lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1001/gvfs Output information may be incomplete. lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1001/gvfs Output information may be incomplete. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME python 12583 jeroenp 7u IPv4 8347396 0t0 TCP 192.168.124.38:54576->192.168.124.23:1012 (ESTABLISHED) python 12583 jeroenp 8u IPv4 8345460 0t0 TCP 192.168.124.38:48250->192.168.124.23:http (CLOSE_WAIT)
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1001/gvfs
is a warning not easy to workaround in a short manner as per [Wayback/Archive] privileges – lsof: WARNING: can’t stat() fuse.gvfsd-fuse file system – Unix & Linux Stack Exchange (thanks [Wayback/Archive] pabouk and [Wayback/Archive] jmunsch):In your case
lsof
does not need to check the GVFS file systems so you can exclude thestat()
calls on them using the-e
option (or you can just ignore the waring):lsof -e /run/user/1000/gvfs
(via: [Wayback/Archive] lsof: WARNING: can’t stat() fuse.gvfsd-fuse file system /run/user/1001/gvfs – Google Search)
So you get this:
# pidof python | tr " " "\n" | xargs -n 1 lsof -i -a -e /run/user/1001/gvfs -p COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME python 12583 jeroenp 7u IPv4 8347396 0t0 TCP 192.168.124.38:54576->192.168.124.23:1012 (ESTABLISHED) python 12583 jeroenp 8u IPv4 8345460 0t0 TCP 192.168.124.38:48250->192.168.124.23:http (CLOSE_WAIT)
lsof
, and xargs
has an argument just for that: -r
, see my earlier post Source: -r
argument to pipe (no argument for MacOS)- If no input is given to xargs, don’t let xargs run the utility – Unix & Linux Stack Exchange, so you get this# pidof python | tr " " "\n" | xargs -r -n 1 lsof -i -a -e /run/user/1001/gvfs -p
Via:
–jeroen
Posted in *nix, *nix-tools, bash, bash, Development, lsof, Power User, Scripting, Software Development, xargs | Leave a Comment »