Get a list of TIDs from a PID in Bash

bash, linux, pid

Solution

I assume this question is linux specific and not unix in general.

`/proc/<PID>/task/` contains a list of directories, one for each thread in the given process.

Problem

Sometimes I want to condition on the existence of a TID (thread identifier) so that I can see when it finishes. For example, suppose I make a large file copy in nautilus. And then I realize it's going to take 3 hours (suppose I realize too late and I don't want to cancel and do it form the CL). I would like to tell my computer to shutdown after Nautilus finishes the copy. If there is big I/O (as of course there will be in a file copy), I can use iotop and that nicely gives me the TID. How can I condition on that TID finishing? Also, suppose a thread isn't doing a lot of I/O. How can I get it's TID? I know how to get PIDs (e.g. `ps` or `cat /proc/<PID>/status`) and when there's only one thread the TID is the same as the PID, but what if there is more than one thread? Here is what I do to condition on a PID: ``` while ps aux | grep [2]9228 > /dev/null; do sleep 20s; done; echo "Process Finished" ```

Original source