2

I am running a script that works on sockets.. It requires sudo to run.. however, Inside the script i call another script that requires not to be run as sudo

here is the code:

import subprocess
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#s.settimeout(5.0)
host='192.168.1.148'
port=1022
s.bind((host, port))
s.listen(5)
while True:
    c, addr = s.accept()
    subprocess.call("python bluetooth2.py",shell=True)
    print 'got connection from',addr
    c.send('Thank you for connecting')
    #c.settimeout(5.0)
    c.recv(1022)
    c.close()

bluetooth2.py runs pulseaudio which is run as root for some reason and doesn't work. any help greatly appreciated!

Here is what the bluetooth2.py script looks like for reference (the one that is calling on pulseaudio)

import time
import pexpect
from sh import bluetoothctl
import subprocess
mac = "C8:84:47:26:E6:3C"
print ("stuck here")
#bluetoothctl("connect", mac)

def connect():
    child = pexpect.spawn('bluetoothctl')
    child.sendline('power on')
    child.sendline('agent on')
    child.sendline('default-agent')
    child.sendline('pair C8:84:47:26:E6:3C')
    time.sleep(1)
    child.sendline('trust C8:84:47:26:E6:3C')
    time.sleep(1)
    child.sendline('connect C8:84:47:26:E6:3C')
    print("connecting...")
    time.sleep(5)
    subprocess.call("pulseaudio --start",shell=True)
    subprocess.call("pacmd set-default-sink
    bluez_sink.C8_84_47_26_E6_3C",shell=True)
    subprocess.call("aplay /home/pi/bleep_01.wav", shell=True)
3
  • Can you maybe import bluetooth2.py directly and use it without creating a new subprocess? Commented Jan 9, 2017 at 2:54
  • Also, look at the following answer: stackoverflow.com/a/567599/177006 It might be relevant with your question. Commented Jan 9, 2017 at 2:57
  • I tried importing the script and i still get the same error from pulseaudio: W: [pulseaudio] main.c: This program is not intended to be run as root (unless --system is specified). Commented Jan 9, 2017 at 3:58

1 Answer 1

2

Solution run PulseAudio for all your users

Add bellow lines into /etc/systemd/system/pulseaudio.service file and save

[Unit]
Description=PulseAudio system server

[Service]
Type=notify
ExecStart=pulseaudio --daemonize=no --system --realtime --log-target=journal

[Install]
WantedBy=multi-user.target
Enable service

sudo systemctl --system enable pulseaudio.service
sudo systemctl --system start pulseaudio.service
sudo systemctl --system status pulseaudio.service

Edit Client conf /etc/pulse/client.conf and replace ass bellow

default-server = /var/run/pulse/native
autospawn = no

Add root to pulse group

sudo adduser root pulse-access

And finally reboot the system

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.