Problems with os.system

A

alexLIGO

Hi,

I am trying to run a python script that executes several other programs
on the bash (under linux) and reads some of the output of those
programs. This works fine for a couple of os.system() calls, but then
it does not work anymore. os.system() returns always -1, but when
executing exactly the same program directly on the linux-bash it works!

Could this be a memory problem? It happens when there is much data
stored in a python list. How can I check the memory usage in my python
script? Can I force python to execute the program on the bash? What can
I do?

I would very appreciate help in that.

Alexander
 
M

marduk

Hi,

I am trying to run a python script that executes several other programs
on the bash (under linux) and reads some of the output of those
programs. This works fine for a couple of os.system() calls, but then
it does not work anymore. os.system() returns always -1, but when
executing exactly the same program directly on the linux-bash it works!

Could this be a memory problem? It happens when there is much data
stored in a python list. How can I check the memory usage in my python
script? Can I force python to execute the program on the bash? What can
I do?

Are you trying to read the output (stdout) of the program? If so you
should be using os.popen() not os.system().
 
A

alexLIGO

No I read some other data files that has been created by the other
program. I am not interested in the stdout or err of that program...
 
M

Michael Hoffman

alexLIGO said:
I am trying to run a python script that executes several other programs
on the bash (under linux) and reads some of the output of those
programs. This works fine for a couple of os.system() calls, but then
it does not work anymore. os.system() returns always -1, but when
executing exactly the same program directly on the linux-bash it works!

Try to break this down to a minimal testcase and then post that.

Also, I usually use the subprocess module instead of os.system these days.
How can I check the memory usage in my python
script? Can I force python to execute the program on the bash?

If bash is your shell, then os.system will call the program through a
bash subshell.
 
J

jepler

Repeated calls to system() seem to cause no problem here.

I ran the following program:
import os

for i in xrange(10000):
assert os.system("true") == 0
in around 25 seconds, the 'for' loop completed, and the 'true' command
always returned 0 from system, as expected.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFDGM2IJd01MZaTXX0RAoLPAJ9WAgv0gd2oasbIk1XCcW3n37OJ6wCgiMA+
nW5kqE1IZqbp7C9wMPd5d9E=
=1rIq
-----END PGP SIGNATURE-----
 
J

jepler

Can I force python to execute the program on the bash? What can
I do?

os.system() is a wrapper on system(3), which invokes /bin/sh.

If you want to use a different shell, you can use
os.spawnv(os.P_WAIT, '/bin/bash', ['bash', '-c', command])
or even avoid the use of a shell altogether:
os.spawnvp(os.P_WAIT, 'myprogram', ['myprogram', '-arg1', 'arg2'])

Jeff


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFDGRVrJd01MZaTXX0RAiUWAJ4j/qcZGVJCVxH1jgVDGp07HS5AdgCfUaaO
kB2XFeUhrFzAwx58pk0S7Rg=
=qHQV
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,115
Latest member
JoshuaMoul
Top