[Tutor] Correct Way to Start a Python Program Under Linux

pytutor.20.247ob at spamgourmet.com pytutor.20.247ob at spamgourmet.com
Fri Jan 19 19:50:12 CET 2007


> <pytutor.20.247ob at spamgourmet.com> wrote
> 
> I'm not sure what you mean by a filelauncher.
> In my window manager I just add the file path to my menu
> by editing a dialog box or config file. which environment are you 
> using?

Im using gnome and creating a 'custom application launcher' by rt click
a panel and choosing 'add to panel'. It is just a case of adding a file
path, the only option is you can run in a terminal (which works but
fires up a terminal window which I do not want)

> > my program uses subprocess.Popen({command string}, etc)
> > and these calls to Popen do not work, only if I run the program
> from 
> > a
> > shell window.
> 
> How do you know it doesn't work? Do you get an error or just silence?
> If you launch a program that writes to stdout then you had better
> be sure it has a stdout to write to! Are you sure its not just
> dumping its output to /dev/null?

when it's working you can see the processes created by doing 'ps -ef'
in another shell window, the python program spawns a child process
running tcpdump.

> OTOH it could also be that the
> import
> path is not set correctly(try checking your PYTHONPATH environment
> variable)

from a shell window typing echo $PYTHONPATH returns nothing, is this
normal, should I be setting this or is it optional? 
from within a python session importing sys and typing > sys.path
I get a long list of paths which I assume is right as its searching and
finding all the standard library modules.

> Can you give us a bit more info about what you mean by the
> statement that it doesn't work?

I mean the call to subprocess.Popen....  should create a new process
when viewed with 'ps -ef' from another command window, normally I see
the new process running the command 'tcpdump .....' but when running
the application from the launcher this just does not happen. The main
pygtk program is running fine, just the new process does not start.

> As a check try writing a very simple program with no external
> dependencies and see if it works...

You have helped crack it, I just wrote a small program like this, it
reads 400 packets from the eth0 interface saving it to a file:

<code>
#!/usr/bin/env python
#test prog
import curses.ascii, sys, os, subprocess, tempfile
def Readtraffic():
    myerr = tempfile.TemporaryFile()
    myout = tempfile.TemporaryFile()
    command = "sudo /usr/sbin/tcpdump -c 400 -q -t -s 192 -i eth0 -w
/var/tmp/testdump -Z wayne"
    myproc = subprocess.Popen(command, executable="/bin/bash",
shell=True, stderr=myerr, stdout=myout)
    return (myproc)

proc = Readtraffic()
stat = proc.poll()
print "stat = " + str(stat)

while stat == None:
    stat = proc.poll()
print "stat = " + str(stat)
print "finished"
</code>

This works fine, using another shell window I can see the subprocess
and tcpdump running under a new pid as expected, it's VERY similar to
the code in the program (which has been chopped about quite a bit by
now) with the difference being I changed the variable 'command' to be a
pure text string instead of a concatenation of variables and strings.
 
Substituting the code above into my program works! 
Now the puzzling bit identifying the actual error,

the code below works until I un-comment the line below '#oldcommand
...', I have even changed the variable name to 'oldcommand' so in
effect it's not even used, but just uncommenting it causes the problem,
ie, the subprocess does not start up when run from an application
launcher (yet still works when started from a shell!).

<code>
#oldcommand = "sudo " + self.exe['tcpdump'] + " -c " + str(packets) + "
-q -t -s " + str(packetsize) \
#+ " -i " + self.nic + " -w " + self.savepath + self.dumpfilename + "
-Z " + os.getlogin()
command = "sudo /usr/sbin/tcpdump -c 400 -q -t -s 192 -i eth0 -w
/var/tmp/testdump -Z wayne"
myproc = subprocess.Popen(command, executable="/bin/bash", shell=True,
stderr=myerr, stdout=myout)
return (myproc)
</code>

After going through the command piece by piece I found the problem, the
call to os.getlogin() works fine when run from a shell BUT fails when
run from a launcher, back to the docs I found:

user = pwd.getpwuid(os.getuid()) [0]

and this works!!!

Thanks for your help,

Wayne.



		
___________________________________________________________ 
What kind of emailer are you? Find out today - get a free analysis of your email personality. Take the quiz at the Yahoo! Mail Championship. 
http://uk.rd.yahoo.com/evt=44106/*http://mail.yahoo.net/uk 


More information about the Tutor mailing list