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.

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.