By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,102 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

Problems with os.system

P: n/a
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

Sep 2 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Fri, 2005-09-02 at 13:45 -0700, alexLIGO wrote:
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().

Sep 2 '05 #2

P: n/a
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...

Sep 2 '05 #3

P: n/a
alexLIGO wrote:
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.
--
Michael Hoffman
Sep 2 '05 #4

P: n/a

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)

iD8DBQFDGM2IJd01MZaTXX0RAoLPAJ9WAgv0gd2oasbIk1XCcW 3n37OJ6wCgiMA+
nW5kqE1IZqbp7C9wMPd5d9E=
=1rIq
-----END PGP SIGNATURE-----

Sep 2 '05 #5

P: n/a
On Fri, Sep 02, 2005 at 01:45:42PM -0700, alexLIGO wrote:
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-----

Sep 3 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.