5

In my process list under Ubuntu (using top/System Monitor) one of the largest memory hogs (200+Mb) was python. I searched a bit for one of my programs to be the cause until I realised this was my Python IDE (Wing), which itself is written in Python.

I thought I could change the name of the program by inserting setproctitle from the setproctitle package, but the python version that Wing is using is different from my own. setproctitle needs to be compiled and the python that wing uses is not a full installation (I asked Wing Support but they are unlikely to change that/incorporate setproctitle).

setproctitle can only change the name of the running process, so I could not make a script that starts Wing and then change the process name either.

After that I tried to write to /proc/PIDNUM/comm, but although that 'file' is 'rw', I am not allowed to write there.

I finally found a, not-so-portable, solution for this particular case. But I would like to know if there is a standard way of changing the process name of another (possible a child-) process with a Linux system call.

Anthon
  • 79,293
  • "setproctitle can only change the name of the running process, so I could not make a script that starts Wing and then change the process name either." It would seem like you could do that, what's to stop you from writing a script that backgrounds the IDE and uses that command to change the proctitle on whatever PID is in $!? – Bratchley Apr 26 '13 at 14:03
  • setproctitle is a C extension module for python that I would need to compile with a python that matches the one used by wing. The python for wing does not come as a full installation and does not allow running python setup.py install. – Anthon Apr 26 '13 at 14:31

3 Answers3

3

A process can only write to its own /proc/pid/comm. So since it sounds like you can modify the IDE's code, you can just have it write to /proc/self/comm.

Another option would be to change the name of its Python executable, and then change all the #! lines, but that may be a PITA.

Other—more painful—options would be writing some C code and using LD_PRELOAD or ptrace.

derobert
  • 109,670
  • That explains why I couldn't test with echo 'abcd' > /proc/PID/comm. The second option is the not so portable solution that I found and refered to. Since there is only one shellscript that loads the python executable that was actually very doable. – Anthon Apr 26 '13 at 17:28
  • I don't think there is a POSIX way of renaming a different process. Renaming yourself is fairly portable with prctl(PR_SET_NAME, …) – derobert Apr 26 '13 at 17:44
3

As derobert already indicated, probably the easiest way to get a different name in the process table is by renaming the 'private' python executable from python to wing_ide.

The startup command, /usr/bin/wing4.1, is actually a minimal shell script calling a second script /usr/lib/wingide4.1/run-wing.sh this I patched as follows:

@@ -66,7 +66,9 @@

 # Location of Python interpreter to use, if not already set
 if [ "${WINGPYTHON}" = "" ]; then
-  if [ -x "${WINGHOME}/bin/PyCore/python" ]; then
+  if [ -x "${WINGHOME}/bin/PyCore/wing_ide" ]; then
+      WINGPYTHON="${WINGHOME}/bin/PyCore/wing_ide"
+  elif [ -x "${WINGHOME}/bin/PyCore/python" ]; then
       WINGPYTHON="${WINGHOME}/bin/PyCore/python"
   elif [ "${WINGIDE_USE_QT4}" = "1" ]; then
       WINGPYTHON="${WINGHOME}/bin/runtime-python2.7/bin/python2.7"

in addition to that I copied the ${WINGHOME}/bin/PyCore/python to ${WINGHOME}/bin/PyCore/wing_ide.

For Wing 5.0 a similar thing can be done but the default python taken after installation is

  WINGPYTHON="${WINGHOME}/bin/runtime-python2.7/bin/python2.7"

so this has to be copied to wing_ide in the same directory and used

Anthon
  • 79,293
2

You can see the entire command used to invoke a process using top, but it doesn't show it by default. Toggle that with lower case c. This should enable you to distinguish one python app from another.

The difference between the command line invocation and the process name shows up in proc too, as cmdline and comm.

goldilocks
  • 87,661
  • 30
  • 204
  • 262