#!/usr/bin/env bash # nixos-config - My current NixOS configuration # # Copyright (C) 2025 Benedikt Peetz # 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 . # 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