Posts: 4
Threads: 1
Joined: May 2018
Hi,
this code was working in Python 2.7.9, and does not work anymore in Python 2.7.13:
1 2 3 4 5 6 |
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
Posts: 4,801
Threads: 77
Joined: Jan 2018
What happens when you run sudo hcitool lescan on the command line (without python)?
Posts: 4
Threads: 1
Joined: May 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).
Posts: 2,953
Threads: 48
Joined: Sep 2016
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) ?
Posts: 4
Threads: 1
Joined: May 2018
I modified the code in order to print the pid and return code:
1 2 3 4 5 6 7 8 9 10 11 |
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
Posts: 2,953
Threads: 48
Joined: Sep 2016
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.
Posts: 566
Threads: 10
Joined: Apr 2017
May-07-2018, 02:14 PM
(This post was last modified: May-07-2018, 02:20 PM by volcano63.)
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.
Test everything in a Python shell (iPython, Azure Notebook, etc.) - Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.
Posts: 4
Threads: 1
Joined: May 2018
May-07-2018, 02:24 PM
(This post was last modified: May-07-2018, 02:24 PM by bruno.)
I made subprocess.Popen 'hcitool lescan' (without sudo), and run the script with sudo prefixed.
Many thanks, this solve the problem !
Posts: 4,801
Threads: 77
Joined: Jan 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.
Posts: 2,953
Threads: 48
Joined: Sep 2016
(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.
|