Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
subprocess.Popen
#1
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
Reply
#2
What happens when you run sudo hcitool lescan on the command line (without python)?
Reply
#3
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).
Reply
#4
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)?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
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
Reply
#6
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
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.
Reply
#8
I made subprocess.Popen 'hcitool lescan' (without sudo), and run the script with sudo prefixed.

Many thanks, this solve the problem !
Reply
#9
(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.
Reply
#10
(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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information subprocess.Popen() suddenly giving me grief? davecotter 3 626 Dec-13-2023, 10:49 PM
Last Post: davecotter
  Use subprocess.Popen and time.sleep chucky831 2 1,954 Aug-11-2022, 07:53 PM
Last Post: carecavoador
  Subprocess.Popen() not working when reading file path from csv file herwin 13 15,106 May-07-2021, 03:26 PM
Last Post: herwin
  Did subprocess.Popen() causes main routine to pause stdout? liudr 4 3,654 May-04-2021, 08:58 PM
Last Post: liudr
  disable subprocess.popen prompt echo paul18fr 1 2,019 Feb-04-2021, 02:50 AM
Last Post: Larz60+
  how to pass the interactive string to Popen subprocess maiya 1 1,884 Sep-18-2020, 09:36 PM
Last Post: Larz60+
  How to get program output from subprocess.Popen? glestwid 1 2,378 Aug-19-2020, 05:44 AM
Last Post: buran
  subprocess.Popen() and encodings voltron 0 5,753 Feb-20-2020, 04:57 PM
Last Post: voltron
  Mock call to subprocess.Popen failing with StopIteration tharpa 7 5,940 Nov-08-2019, 05:00 PM
Last Post: Gribouillis
  no coding= option fo subprocess.Popen() Skaperen 5 5,731 Jun-21-2019, 10:24 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020