Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Last active January 2, 2019 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeoncross/66013ebcd7f51790c75db223fb33e471 to your computer and use it in GitHub Desktop.
Save xeoncross/66013ebcd7f51790c75db223fb33e471 to your computer and use it in GitHub Desktop.
Counting file descriptors used by a process or Go application
package main
func countOpenFiles() int {
out, err := exec.Command("/bin/sh", "-c", fmt.Sprintf("lsof -p %v | wc -l", os.Getpid())).Output()
if err != nil {
// log.Println("count files", err)
return 0
}
n, err := strconv.Atoi(strings.TrimSpace(string(out)))
if err != nil {
return 0
}
return n
}
# Process ID's using the most file descriptors
lsof | awk '{ print $2; }' | uniq -c | sort -rn | head
# Name of process + PID
sudo lsof | awk '{ print $1" "$2; }' | uniq -c | sort -rn
# OR
# Find the PID
ps aux | grep yourappname
# Count the file descriptors for this process
lsof -p PIDHERE | wc -l to count
@xeoncross
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment