0

I am passing a user input using python subprocess using input in subprocess.run

dargs = ['java','-jar','cli.jar','ion','-cf', config_path, '-r', deldata.get("reg"), '-n', deldata.get("space"), '-e', deldata.get("tion"), '-c', listargs[2], '-f', 'true']
dresp = subprocess.run(dargs, text=True, capture_output=True, input="yes")

This returns following error :

returned non-zero exit status 1.
When calling: subprocess.run(dargs, text=True, capture_output=True, check=True, input="yes")

but the same if I do from terminal it works fine

java -jar cli.jar ion -r DUB -n oa -cf /config/path -e test -c ht -f true

The above command outputs the following and asks for user input like this in terminal

this is force operation.
V Id: sl, Desc: ht
Confirm operation: (yes/no)yes #----->here comes my user input to terminal
Operation done successfully

command triggered from terminal first outputs this two lines

this is force operation.
V Id: sl, Desc: ht 

Then waits for sometime and then asks for input Confirm operation: (yes/no)

Not sure how to make subprocess not pipe and send the yes input rather wait for prompt and check for this line Confirm operation: (yes/no) and then provide the input as yes or no

I believe with my current code its performing yes | java -jar cli.jar ion -r DUB -n oa -cf /config/path -e test -c ht -f true kind of operation which is not excepted by cli.

Any help on how to achieve this will be great

Suzana
  • 13
  • 4
  • You can try using parts from https://stackoverflow.com/a/48787335/7916438, it shows how to send input to a `subprocess`. I'm not sure about the output part, in a terminal it may not be necessary to redirect it at all and it would just simply appear. – tevemadar Nov 26 '21 at 13:46
  • @tevemadar as far as I am aware providing input="whatever" works right ? and the reference you are providing that is of subprocess.popen – Suzana Nov 26 '21 at 13:52

1 Answers1

0

Try passing a \rat the end os the string in the input argument ( input="yes\r", ...). Also, this depends on "cli.jar" being in the same folder the Python script is, and calling the Python script from that folder. Adding the full absolute path to "cli.jar" will help.

Funny thing is that without passing "check=True" to subprocess.run, the program should not break: you'd just get the failed run information on your dresp program, and you can print that to get whatever information the program you tried to run printed out (due to capture_output=True).

If it does not work, try to break your case in steps, like, try to run java -version first, so you can assert the java interpreter is being located and run successfully. Also, if your program is not breaking at the call, just add a print(dresp) there.

jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • I have kept the cli.jar in same path that works fine in other subprocess commands that I am running where there is no need to pass the input. I have removed `check=True` java interpreter also works fine as other subprocess commands are running. rather when I added `yes\r` in input it does take the input now but it looks like the process is completed and then it provides the value to subprocess..atleast it is not exiting with the previous error but still it doesn't solve the problem – Suzana Nov 26 '21 at 14:45
  • is it possible to pass input after some delay ? – Suzana Nov 26 '21 at 15:06
  • I figured out the issue but not sure what is the solution actually it fails if I pipe the input and send. In my above code subprocess is doing this same as in this terminal I believe `yes | java -jar cli.jar ion -r DUB -n oa -cf /config/path -e test -c ht -f true` cli has restrictions in execution like this any workaround in python for this to pass the input? – Suzana Nov 26 '21 at 17:55