4

So, I know everyone is going to tell me to use the subprocess module, but I can't use that for the project I am working on since Piping simply doesn't want to work with wxpython and py2exe on my system.

So, I've been using the os.system call. I need to know how to wait for the process to end. Currently, I have

os.system(cmd)

and my command may actually take a long time to execute, so it usually times out early. How can I make my program to wait for os.system? I've tried waitpid and I guess that doesn't work for os.system.

I am developing for windows so I can't use fork and execvp unfortunately. I have a lot of hands tied :(

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user2434493
  • 41
  • 1
  • 2
  • 2
    os.system should not ever time out, and should not return until the process is complete. Perhaps your command is timing out? – AMADANON Inc. Jul 25 '13 at 20:53
  • related: [subprocess with timeout](http://stackoverflow.com/q/1191374/4279) – jfs Oct 15 '15 at 09:59

2 Answers2

2

you can correct your code:

os.system('cmd')

extra explain about subprocess:

import subprocess
ls_output = subprocess.check_output(['ls'])

Running External Command

To run an external command without interacting with it, such as one would do with os.system(), Use the call() function.

import subprocess

# Simple command
subprocess.call('ls -l', shell=True)

$ python replace_os_system.py
total 16
-rw-r--r--   1 root8085  root8085     0 Jul  1 13:27 __init__.py
-rw-r--r--   1 root8085  root8085  1316 Jul  1 13:27 replace_os_system.py
-rw-r--r--   1 root8085  root8085  1167 Jul  1 13:27 replace_os_system.py~

# run cmd

import subprocess
l = subprocess.call(['cmd'])

Extra example: Make a system call three different ways:

#! /usr/bin/env python
import subprocess
# Use a sequence of args
return_code = subprocess.call(["echo", "hello sequence"])

# Set shell=true so we can use a simple string for the command
return_code = subprocess.call("echo hello string", shell=True)

# subprocess.call() is equivalent to using subprocess.Popen() and wait()
proc = subprocess.Popen("echo hello popen", shell=True)
return_code = proc.wait() # wait for process to finish so we can get the return code

Control stderr and stdout:

#! /usr/bin/env python
import subprocess
# Put stderr and stdout into pipes
proc = subprocess.Popen("echo hello stdout; echo hello stderr >&2", \
        shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
return_code = proc.wait()
# Read from pipes
for line in proc.stdout:
    print("stdout: " + line.rstrip())
for line in proc.stderr:
    print("stderr: " + line.rstrip())
user4020527
  • 1
  • 8
  • 20
0

Has answered similar question in another post.

key point is:

use subprocess.check_output to instead os.system, for subprocess.check_output support timeout:

subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, cwd=None, encoding=None, errors=None, universal_newlines=None, timeout=None, text=None, **other_popen_kwargs)

crifan
  • 12,947
  • 1
  • 71
  • 56