[Baypiggies] TypeError in os.path.realpath

Mitch Patenaude patenaude at gmail.com
Wed May 30 20:05:20 CEST 2012


I believe the problem is that /proc/<PID>/cmd has the whole command string,
including arguments, separated by NULLs (\x00).  The commands without
arguments just look like a null terminated string, but the ones with
arguments have nulls interspersed in the middle of the string.  You
probably want to split on \x00, and then take only the first element of the
tuple.

  --  Mitch


On Wed, May 30, 2012 at 9:33 AM, Lincoln Peters <anfrind at gmail.com> wrote:

> I'm developing a Python tool to monitor some of our Linux servers (running
> RHEL 5.6 and Python 2.4).  In some cases, I need to track down the
> executable file for a running process, which I should be able to by
> resolving the symlink at /proc/<PID>/cmd.  When I use the "readlink"
> command at the shell, it works correctly, but sometimes when I call
> os.path.realpath with the exact same path, I get a TypeError the following
> traceback (note: copied by hand since the servers have no Internet access):
>
> File "/usr/lib64/python2.4/posixpath.py", line 423, in realpath
>   resolved = _resolve_link(component)
> File "/usr/lib64/python2.4/posixpath.py", line 440, in _resolve_link
>   while islink(path):
> File "/usr/lib64/python2.4/posixpath.py", line 159, in islink
>   st = os.lstat(path)
> TypeError: lstat() argument 1 must be (encoded string without NULL bytes),
> not str
>
> Strangely, this only happens with some processes and not others, and I
> can't discern any pattern.  Any thoughts?
>
> Thanks in advance.
>
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20120530/bc141a84/attachment.html>


More information about the Baypiggies mailing list