Personal tools
You are here: Home Linux and free software How to make PulseAudio run once at boot for all your users

How to make PulseAudio run once at boot for all your users

Posted by Rudd-O at Jul 13, 2009 08:25 AM |
Filed under: , , , , ,

Running PulseAudio as a system-wide service has advantages -- you can play audio without having logged on, multiple users can play audio on the same audio gear, and music daemons like MPD won't fight for the audio device with PulseAudio. Here's how.

These instructions should work in Fedora 11, and probably Ubuntu as well.  You will probably lose the ability to pair up Bluetooth audo gear, like lose  autotunneling capabilities to networked audio devices through Zeroconf, but hey, you get what you pay for ;-)  I also hear there are security considerations stemming from the fact that users allowed access to PulseAudio may break something in it, but for home use it is probably going to be just fine.

Starting up PulseAudio on boot

Put this text in the file /etc/event.d/pulseaudio:

start on runlevel [2345]
stop on runlevel [016]

exec pulseaudio --system --realtime --log-target=syslog

respawn

description "PulseAudio system server"

pre-start script
	for a in `seq 60` ; do
		sleep 1
		killall -0 hald && killall -0 dbus-daemon && break
	done
end script

post-start script
	( sleep 1 ; paplay /usr/share/sounds/login.wav ) &
end script

The text in italics is optional -- either don't include it, or replace it with something that will play a sound file confirming that PulseAudio has started.

As you can see, this is a straightforward script that waits until hald and D-Bus are active before starting PulseAudio (both are needed to get the hardware list), and then starts it up.

Setting up default client settings

Add these lines in the file /etc/pulse/client.conf:

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

Allowing users access to the audio service

Add every user that is allowed to use the audio device to the group pulse-access.

Allowing PulseAudio access to Bluetooth

In order to pair up with Bluetooth devices, you need to add the following text to the file /etc/dbus-1/system.d/pulse.conf:

<!DOCTYPE busconfig PUBLIC
 "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
	<policy user="root">
		<allow own="org.pulseaudio.Server"/>
                <allow send_destination="org.bluez"/>
                <allow send_interface="org.bluez.Manager"/>
	</policy>
	<policy user="pulse">
		<allow own="org.pulseaudio.Server"/>
                <allow send_destination="org.bluez"/>
                <allow send_interface="org.bluez.Manager"/>
	</policy>
	<policy context="default">
                <deny own="org.pulseaudio.Server"/>
                <deny send_destination="org.bluez"/>
                <deny send_interface="org.bluez.Manager"/>
        </policy>
</busconfig>

Preventing PulseAudio from starting on graphical logon

A pesky program tries to start PulseAudio every time you log on, which may cause conflicts.  Prevent this by running the following command as root:

chmod -x /usr/bin/start-pulseaudio-x11

You may need to run this command whenever you upgrade your system and, in the course of the upgrade, PulseAudio itself is upgraded.

I've submitted a patch for PulseAudio that will render this step unnecessary soon, if it's accepted. It's logged as ticket 606 in the PulseAudio bug tracker.

That's it!

Close your session and reboot your machine.  PulseAudio should start on boot now, and all applications will be able to play sound, whether they are console or graphical applications, and without the need to actually log on beforehand.  In addition, if PulseAudio dies, it will be automatically restarted and you will hear a confirmatory sound (provided you actually put the italicized text with a command to play audio).

Document Actions

Doesn't work

Avatar Posted by Fibonacci Prower at Jul 20, 2009 07:39 PM
Just tried your instructions on Ubuntu. It created precisely the problems it was supposed to solve.
First of all there was no audio before logging in. Second, the audio was jerky and grainy (as if I had sand on my speakers) during the first few minutes of play. Third and worst of all, after some short time (never more than 8 minutes) the daemon would die and terminate all audio streams currently running; yes, it is immediately restarted, but then I have to restart all programs which were playing audio before - such as Xine.

So I just undid all of your changes (except that the users remained in the group pulse-access), and those problems have disappeared from PA.

This is ODD

Avatar Posted by Rudd-O at Jul 20, 2009 07:43 PM
The audio was grainy? I am not exactly sure what happened there, but I don't have an hypothesis of the problem you experienced, so it would be helpful if you could diagnose the issue a bit more in depth. It seems to me as if your PulseAudio server was "staggering" or "lagging" for some reason that I can't understand. I need to know:

- whether there was only one PulseAudio server running on your machine, or autospawn actually started two (it does when it can't connect to an already-running one)
- whether your system pulseaudio instance was running with real-time priority or elevated nice level
- whether your audio clients were actually talking to the system pulseaudio server (this can easily be verified by launching paman, checking that it is connected to /var/run/pulse/native, and finally checking the clients list as you start and stop audio playback on your apps -- you should see them pop into that list)

Also, elevated logging to syslog and a capture of that would be very helpful. Increase the pulseaudio log level in the daemon.conf file, or in the command-line parameter you use to launch it in the event.d file. You could also launch it as root in a terminal and with log output to the terminal itself, elevated logging level, and no-daemon so it doesn't go to the background.

Ubuntu 9.10 (Karmic Koala) works great

Avatar Posted by Mathias Rav at Nov 09, 2009 06:45 PM
I had no problems setting this up. Except for /etc/event.d, I put the conf in /etc/init, which is the convention in Ubuntu Karmic apparently. I changed /etc/pulse/client.conf, removed exec from start--x11 and added myself to the pulse-access group, and now I have music playing with mpd even when I log out or switch to a textual tty.

Thanks a lot!

(By the way, anonymous posting doesn't seem to work, even though it says so when adding a comment. Is that a glitch?)

Excellent!

Avatar Posted by Rudd-O at Nov 09, 2009 06:46 PM
By the way, if you can post your upstart initscript here, that would be great for future readers! :-)

Upstart script

Avatar Posted by Mathias Rav at Nov 09, 2009 06:54 PM
I used the script from your post verbatim, but instead of /etc/event.d I put it in the file /etc/init/pulseaudio.conf. Syntax is the same.

Fabulous!

Avatar Posted by Rudd-O at Jan 04, 2010 02:08 AM
Thanks!

Add comment

You can add a comment by filling out the form below. Plain text formatting.

Info
You are not logged in. You may optionally enter your username and password below. If you don't enter anything, this comment will be posted as 'Anonymous User'.
(Required)
(Required)
(Required)
Enter the word