linux查看线程执行状态+python获取线程id

ubuntu(linux)跟踪指定进程的线程执行状态的方法
新建一个用于测试的py文件,内容如下


# coding: utf-8
import threading
import time
import os
import ctypes
def func(arg):
    while True:
        time.sleep(1)
        print('thread:{}, arg={}, pid={}, ppid={}'.format(threading.get_ident(), arg,os.getpid(),   ctypes.CDLL('libc.so.6').syscall(186)))
    return 0

if __name__ == '__main__':
    threading.Thread(target=func, name='eat', args=(1,),daemon=True).start()
    threading.Thread(target=func, name='run', args=(2,),daemon=True).start()

    while True:
        time.sleep(1)


1、首先使用ps -aux | grep “PROGRAM_WORD”命令去过滤你要查看的进程
获取进程id,如下图:
在这里插入图片描述

2、使用 ps -T -p 27850
在这里插入图片描述

3、使用 strace -p 27851
在这里插入图片描述

注意:如果在docker中无法执行strace命令,则启动docker时增加–priveleged参数即可,例如:docker exec --priveleged -it CONTAINER_NAME bash

另外,本文提供了使用python获取线程id的方法,使用ctypes加载C标准库,然后再通过系统调用获取线程id。

猜你喜欢

转载自blog.csdn.net/cy_shichao/article/details/85104239