Is there a standard format for files in /proc/$PID
?
I ask because I want to retrieve some process info from /proc
directory. For example, there is this proc/$PID/status
file with Name:'\t'ProcName
in its first line. Can I meet this file elsewhere with a whitespace instead of \t
, or something like that?
First of all, the documentation on /proc
in Linux is provided in the Linux sources, in Documentation/filesystems/proc.txt
. That should be the first place you look into if going to work with procfs. Sadly, AFAICS it doesn't mention the exact record format.
The second place to look is procps
sources (that is, the package which provides ps
tool). There you can find:
colon = strchr(S, ':');
if(unlikely(!colon)) break;
if(unlikely(colon[1]!='\t')) break;
which means that ps
relies on the :\t
being there. Therefore, you can assume that all current Linux kernels use this format. Moreover, I doubt that minor changes (like replacing the \t
with something else) would be considered important enough to break compatibility with the old versions of ps
tool.
That said, you can usually be more liberal in what you accept. Considering the specific contents of that file, you can assume the colon being field separator, and strip any whitespace following it. If you are using shell script, the regular field separation should suffice.
Lastly, I'd like to make a few points:
status
file is supposed to be human-readable. Therefore, programs are usually better reading the stat
file instead which is designed to be machine-oriented./proc
format.libprocps
library which comes with procps
instead of reading the files by hand. That way, you avoid re-inventing the wheel and relying on a specific format directly.