Check the open FD limit for a given process in Linux

后端 未结 7 1850
粉色の甜心
粉色の甜心 2020-12-04 10:13

I recently had a Linux process which “leaked” file descriptors: It opened them and didn\'t properly close some of them.

If I had monitored this, I could tell – in adv

7条回答
  •  佛祖请我去吃肉
    2020-12-04 10:57

    Python wrapper using the excellent psutil package:

    import psutil
    
    for p in psutil.process_iter(attrs=['pid', 'name', 'username', 'num_fds']):
        try:
            soft, hard = p.rlimit(psutil.RLIMIT_NOFILE)
            cur = p.info['num_fds']
            usage = int(cur / soft * 100)
            print('{:>2d}% {}/{}/{}'.format(
                usage,
                p.info['pid'],
                p.info['username'],
                p.info['name'],
                ))
        except psutil.NoSuchProcess:
            pass
    

提交回复
热议问题