1

I want run a process called client.sh in gnome-terminal via python script and want to pass the arguments as input to execute it. Below is my code

import os
import subprocess
import time
from subprocess import Popen,PIPE
import pexpect
process = subprocess.Popen('sudo gnome-terminal  -x ./client.sh', stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
process.communicate(input = "1") 

my intension is to send input as "1" to client.sh process. But with above code my process[client.sh] didn't get any input. How can i send inputs to my sub process process?.

1 Answers1

0

You can just call the command with the argument:

process = subprocess.Popen(['sudo gnome-terminal  -x ./client.sh', '1'], stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False)

Be carefull when using shell=True, see this.

If you need to "emulate" an argument input, use:

process = subprocess.Popen(['sudo gnome-terminal  -x ./client.sh'], stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False)

and then:

process.stdin.write("1" + "\n")
matt.LLVW
  • 502
  • 4
  • 14
  • I tired as you suggest . Getting the below error if i make shell = False process = subprocess.Popen(['sudo gnome-terminal -x ./client.sh','1'], stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False) File "/root/anaconda2/lib/python2.7/subprocess.py", line 390, in __init__ errread, errwrite) File "/root/anaconda2/lib/python2.7/subprocess.py", line 1025, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory – M.Girish Babu Apr 07 '18 at 12:43
  • see my edit. Also, try using the full path, i.e: `/home/foo/client.sh` – matt.LLVW Apr 07 '18 at 12:47