0

Seems duplicate of Using Python to open a shell environment, run a command and exit environment. I want to run the ulimit command in the shell environment in Redhat. Procedure: Open shell environment, run ulimit commands on shell, get the result and exit the shell environmnet. Referencing the above solution, I tried:

from subprocess import Popen, PIPE
def read_limit():
    p = Popen('sh', stdin=PIPE)
    file_size = p.communicate('ulimit -n')
    open_files = p.communicate('ulimit -f')
    file_locks = p.communicate('ulimit -x')
    return file_size, open_files, file_locks

But I got error: ValueError: I/O operation on closed file.

gd1
  • 655
  • 1
  • 10
  • 21

1 Answers1

0

The documentation for communicate() says:

send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate.

After that, the pipe will be closed.

You can use

p.stdin.write("something")
p.stdin.flush()
result = p.stdout.readline()

for your three commands and then

p.stdin.close()
p.wait()

at the end to terminate it

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222