Search code examples
linuxbashprocessscriptingps

Bash: Easy way to know whether there is a thread with specified thread id?


I need to know if there is a thread with specified thread id. Probably I can look at all process's threads and see if it exists, or get all thread list with ps command and try to find thread in output, but isn't there a simpler way?


Solution

  • I'm assuming you are on Linux?

    If you know the process id you can use the proc filesystem to figure out whether a certain thread exists or not.

    Example:

    Process ID $PID 14456

    Thread ID $TID 14465

    If the directory /proc/$PID/task/$TID exists, the thread is running.

    If you don't know the process ID you can let the shell do the globbing for you:

    if [ -d /proc/*/task/$TID ]; then echo found; fi