How to debug "too many open files"? I have a program that quits after a time with "too many open files". How can I trace which files are opened and closed? I know I can set this limit a higher value, but that would only defer the problem.

I remember using a program like dtruss or dtrace for that purpose but I can't remember how I invoked it and --help gave to much confusion.

I run that program with a wrapper script based on nodejs, so I need to follow the child processes.

Question: what is the best strategy to find out which files (descriptors) are opened and which are closed?
lsof will show you the open file handles.

If you know the PID use the -p flag.

Eg, on my zsh shell at the moment I see:

alex@smiley:~|⇒  lsof -p 82946
COMMAND   PID USER   FD   TYPE DEVICE  SIZE/OFF     NODE NAME
zsh     82946 alex  cwd    DIR  14,12      2414   186278 /Users/alex
zsh     82946 alex  txt    REG  14,12   1103984       73 /bin/zsh
zsh     82946 alex  txt    REG  14,12    474672   787843 /usr/lib/zsh/4.3.11/zsh/zle.so
zsh     82946 alex  txt    REG  14,12    258672   787864 /usr/lib/zsh/4.3.11/zsh/complete.so
zsh     82946 alex  txt    REG  14,12    120576   787863 /usr/lib/zsh/4.3.11/zsh/complist.so
zsh     82946 alex  txt    REG  14,12     71264   787838 /usr/lib/zsh/4.3.11/zsh/zutil.so
zsh     82946 alex  txt    REG  14,12     79968   787852 /usr/lib/zsh/4.3.11/zsh/parameter.so
zsh     82946 alex  txt    REG  14,12    599280  7719861 /usr/lib/dyld
zsh     82946 alex  txt    REG  14,12 296980480 12427083 /private/var/db/dyld/dyld_shared_cache_x86_64
zsh     82946 alex    0u   CHR   16,0   0t26354     6285 /dev/ttys000
zsh     82946 alex    1u   CHR   16,0   0t26354     6285 /dev/ttys000
zsh     82946 alex    2u   CHR   16,0   0t26354     6285 /dev/ttys000
zsh     82946 alex   10u   CHR   16,0    0t3432     6285 /dev/ttys000


The Type column shows the various file or pseudo file types that might be open, sockets, pipes, etc.

ulimit or limit, depending on system and shell lets you change the number of open files.