The problem with reading /proc file system is that it is not atomic. I tried using a library (procfs in Python) that wishes to read it, but it has glitches about 5% of the time because the process that I am after is now gone. So, I wish to copy everything in /proc to another location, say
/proc_clone/<GUID>/. A GUID can be generated using the
The naive approach would be trying to use
cp -r, but I am getting a whole bunch of 'permission denied' errors and it is taking more than a second. I am only after a subset
/proc/<pid> and even then, only the read-only stuff that can be turned into a string (ultimately I wish to build a single JSON file that contains all of this).
I am looking for a script that would do that for me and is also short, simple, very fast, can deal with processes dying mid-flight by skipping them, however, I would like to avoid writing C code - I would much rather stick to bash, existing utilities and maybe Perl / Python. The goal here is just to take a snapshot of a subset of
/proc, not produce the JSON file itself.
I have heard opinions that "one should not ever try to copy
/proc but just read from it". Well, the non-atomic nature of it means that one has to throw a bunch of
try/catch all over the code when trying to do very simple operations. Using a high level language like Python (what I ultimately want to use) to iterate over
/proc is a slow method, which involves both IO and CPU time. This increases the risk of seeing a process die on you (I certainly see it often; I have a script that interrogates /proc every minute and I run into plenty of exceptions). I would like to build a library that outputs a single JSON file that contains cpu and memory usage information about processes in a human-readable format (e.g. using seconds and not jiffies, bytes and not pages, have units in addition to values. I am not concerned that it would take some time to create that file out of a directory dump; I just want to make sure that the snapshot is as accurate as possible. So. if I should not just copy /proc over, then which other method should I use?