Python Forum
subprocess.Popen - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: subprocess.Popen (/thread-9965.html)

Pages: 1 2


subprocess.Popen - bruno - May-07-2018

Hi,

this code was working in Python 2.7.9, and does not work anymore in Python 2.7.13:

command = "sudo hcitool lescan"
process = subprocess.Popen(command.split(), stdout = subprocess.PIPE)
sleep(3)
os.kill(process.pid, signal.SIGINT)
data = process.communicate()[0]
print(data)
So how can I get the command response ?

Thanks for help, best regards


RE: subprocess.Popen - Gribouillis - May-07-2018

What happens when you run sudo hcitool lescan on the command line (without python)?


RE: subprocess.Popen - bruno - May-07-2018

When I run the command from the command line, I get what I expects: a list of bluetooth devices (then I have to stop the process with CTRL+C after few seconds, because the process continue to search for other devices).


RE: subprocess.Popen - wavic - May-07-2018

Not working is not enough information what is going on.

Does print function prints something or not? Did you check the status code: print(process.returncode)?


RE: subprocess.Popen - bruno - May-07-2018

I modified the code in order to print the pid and return code:

command = "sudo hcitool lescan"
process = subprocess.Popen(command.split(), stdout = subprocess.PIPE)
sleep(3)
print("pid = ")
print(process.pid)
print("return code = ")
print(process.returncode)
os.kill(process.pid, signal.SIGINT)
print("data = ")
data = process.communicate()[0]
print(data)
I get the following:

pid =
668
return code =
None
data =

but no data is printed, and the process is not killed, because I don't have the prompt


RE: subprocess.Popen - wavic - May-07-2018

You are using sudo but how do you provide the password?

Instead try to subprocess.Popen 'hcitool lescan' and run the script itself with sudo prefixed.


RE: subprocess.Popen - volcano63 - May-07-2018

If you open a process with sudo - you have to kill it with sudo

(May-07-2018, 01:58 PM)wavic Wrote: You are using sudo but how do you provide the password?

Instead try to subprocess.Popen 'hcitool lescan' and run the script itself with sudo prefixed.

If your user is in the list of "sudoers", you may run "sudo" without a password. Depends on environment definition.


RE: subprocess.Popen - bruno - May-07-2018

I made subprocess.Popen 'hcitool lescan' (without sudo), and run the script with sudo prefixed.

Many thanks, this solve the problem !


RE: subprocess.Popen - Gribouillis - May-07-2018

(May-07-2018, 02:24 PM)bruno Wrote: I made subprocess.Popen 'hcitool lescan' (without sudo), and run the script with sudo prefixed.
You can also probably use the trick I showed in this thread (replace the 'ls' command with 'hcitool lescan'). This may be better than running the whole script with the sudo command.


RE: subprocess.Popen - wavic - May-07-2018

(May-07-2018, 02:14 PM)volcano63 Wrote: If you open a process with sudo - you have to kill it with sudo

(May-07-2018, 01:58 PM)wavic Wrote: You are using sudo but how do you provide the password?

Instead try to subprocess.Popen 'hcitool lescan' and run the script itself with sudo prefixed.

If your user is in the list of "sudoers", you may run "sudo" without a password. Depends on environment definition.

Yes but this is not quite safe. I imagine the day I am away from the laptop and my little sister starts typing whatever she wants. Cry