about summary refs log tree commit diff stats
path: root/pkgs/by-name/fd/fd_list/fd_list.sh
blob: 568d73c898e7bbb03dcafd0017a62494caeb0095 (plain) (blame)
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
32
33
34
35
36
37
38
39
40
#!/usr/bin/env bash

# nixos-config - My current NixOS configuration
#
# Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This file is part of my nixos-config.
#
# You should have received a copy of the License along with this program.
# If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.

# Lists all fds in use with their respective associated programs.
# This can be useful, if you need to debug, why your system is out of fds.

# This script is not POSIX shell compatible, as the `ulimit` flags `-H` and `-n` are not defined by POSIX (thus we use `bash` instead).

find /proc/ -maxdepth 1 | while read -r path; do
    pid="${path#/proc/}"
    fd_dir="$path/fd/"

    [ -d "$fd_dir" ] && echo "PID = $pid with $(find "$fd_dir" | wc -l) file descriptors"
done | sort -rn -k5 | head | while read -r _ _ pid _ fdcount _; do
    command_execution="$(ps -o cmd -p "$pid" -h)"

    command_arguments="$(echo "$command_execution" | awk '{$1=""; print $0}')"
    command_name="$(basename "$(echo "$command_execution" | awk '{print $1}')")"

    command="$command_name$command_arguments"

    printf "PID %8d with %4d file descriptors: %s\n" "$pid" "$fdcount" "$command"
done || true
# ^ Ignore SIGPIPE

printf "\nThe Kernel has %7d file handles allocated.\n" "$(awk '{print $1}' /proc/sys/fs/file-nr)"
printf "    -> The per-process fd maxium is: %d\n" "$(ulimit -Hn)"
printf "    -> The per-process fd soft-maximum is: %d\n" "$(ulimit -n)"
printf "    -> The global fd maximum is: %d\n" "$(cat /proc/sys/fs/file-max)"

# vim: ft=sh