How does PulseAudio start?

It seems any process linking to the libpulse* family of shared objects--either before or after running X and the i3 window manager--may implicitly autospawn PulseAudio server, under your user process, as a byproduct of attempts to interface with the audio subsystem. PulseAudio creator Lennart Poettering seems to confirm this, in a 2015-05-29 email to the systemd-devel mailing list:

"pulseaudio is generally not a system service but a user service. Unless your user session is fully converted to be managed by systemd too (which is unlikely) systemd is hence not involved at all with starting it.

"PA is usually started from the session setup script or service. In Gnome that's gnome-session, for example. It's also auto-spawned on-demand if the libraries are used and note that it is missing."

For example, on Debian Stretch (Testing), web browser IceWeasel links to two libpulse* shared objects: 1) libpulsecommon-7.1.so; and 2) libpulse.so.0.18.2:

k@bucket:~$ ps -ef | grep iceweasel
k        17318     1  5 18:58 tty2     00:00:15 iceweasel
k        17498  1879  0 19:03 pts/0    00:00:00 grep iceweasel
k@bucket:~$ sudo pmap 17318 | grep -i pulse
00007fee08377000  65540K rw-s- pulse-shm-2442253193
00007fee0c378000  65540K rw-s- pulse-shm-3156287926
00007fee11d24000    500K r-x-- libpulsecommon-7.1.so
00007fee11da1000   2048K ----- libpulsecommon-7.1.so
00007fee11fa1000      4K r---- libpulsecommon-7.1.so
00007fee11fa2000      8K rw--- libpulsecommon-7.1.so
00007fee121af000    316K r-x-- libpulse.so.0.18.2
00007fee121fe000   2044K ----- libpulse.so.0.18.2
00007fee123fd000      4K r---- libpulse.so.0.18.2
00007fee123fe000      4K rw--- libpulse.so.0.18.2

You may see which running processes link to libpulse*. For example, first get a list of libpulse* shared objects, then run lsof on each (note: this comes from Debian Stretch (Testing), so your output may differ):

sudo find / -type f -name "*libpulse*"
*snip*
/usr/lib/x86_64-linux-gnu/pulseaudio/libpulsedsp.so
/usr/lib/x86_64-linux-gnu/pulseaudio/libpulsecommon-7.1.so
/usr/lib/x86_64-linux-gnu/libpulse.so.0.18.2
/usr/lib/x86_64-linux-gnu/libpulse-simple.so.0.1.0
/usr/lib/x86_64-linux-gnu/libpulse-mainloop-glib.so.0.0.5
/usr/lib/libpulsecore-7.1.so
/usr/lib/ao/plugins-4/libpulse.so

sudo lsof /usr/lib/x86_64-linux-gnu/pulseaudio/libpulsecommon-7.1.so
COMMAND     PID       USER  FD   TYPE DEVICE SIZE/OFF   NODE NAME
gnome-she   864 Debian-gdm mem    REG  252,1   524312 274980 /usr/lib/x86_64-linux-gnu/pulseaudio/libpulsecommon-7.1.so
gnome-set   965 Debian-gdm mem    REG  252,1   524312 274980 /usr/lib/x86_64-linux-gnu/pulseaudio/libpulsecommon-7.1.so
gnome-set  1232          k mem    REG  252,1   524312 274980 /usr/lib/x86_64-linux-gnu/pulseaudio/libpulsecommon-7.1.so
gnome-she  1286          k mem    REG  252,1   524312 274980 /usr/lib/x86_64-linux-gnu/pulseaudio/libpulsecommon-7.1.so
chrome     2730          k mem    REG  252,1   524312 274980 /usr/lib/x86_64-linux-gnu/pulseaudio/libpulsecommon-7.1.so
pulseaudi 18356          k mem    REG  252,1   524312 274980 /usr/lib/x86_64-linux-gnu/pulseaudio/libpulsecommon-7.1.so

To tell these processes not to autospawn PulseAudio, edit ~/.config/pulse/client.conf and add line

autospawn = no

PulseAudio and its libraries respect that setting, generally.

The libpulse* linking by running processes may also indicate why PulseAudio respawns so quickly. The FreeDesktop.org page, "Running PulseAudio", seems to confirm this:

"...typically some background application will immediately reconnect, causing the server to get immediately restarted."

You seem to indicate you start the i3 window manager via the console (by running xinit) and do not use a display manager or desktop environment. The rest of this answer details info for those that do use GNOME, KDE, and so forth.

ADDITIONAL INFO, FOR GNOME/KDE AUTOSTART

Package PulseAudio (5.0-13), in Debian Jessie (Stable) amd64, installs the following four system files:

  1. /etc/xdg/autostart/pulseaudio-kde.desktop
  2. /etc/xdg/autostart/pulseaudio.desktop
  3. /usr/bin/start-pulseaudio-x11
  4. /usr/bin/start-pulseaudio-kde

Some graphical session managers automatically run FreeDesktop.org autostart scripts on user login. The PulseAudio autostart script, in turn, tells graphical session managers to run the appropriate PulseAudio startup script:

/usr/bin/start-pulseaudio-x11
/usr/bin/start-pulseaudio-kde

These scripts call PulseAudio client /usr/bin/pactl to load PulseAudio modules, which spawns the PulseAudio server as a byproduct (note: if you have autospawn set to "no", pactl respects that and will not autospawn PulseAudio server).

More detail, at the FreeDesktop.org page "Running PulseAudio".

Some display managers, in addition and in other distributions, may start PulseAudio (for example, SDDM, on ArchLinux. Though maintainers may have resolved this, by now).


How is PulseAudio getting started?

While this is still a valid question, the answer for it has changed over time. The previous answers are either insufficient or incorrect as now PulseAudio is being launched automatically by the user's systemd, which in turn was launched by the system's systemd when the user logged in.

$ systemctl --user status pulseaudio
● pulseaudio.service - Sound Service
   Loaded: loaded (/usr/lib/systemd/user/pulseaudio.service; enabled; vendor pre
   Active: active (running) since Tue 2020-03-14 15:29:56 ZULU; 13s ago
 Main PID: 31080 (pulseaudio)
   CGroup: /user.slice/user-1000.slice/[email protected]/pulseaudio.service
           ├─31080 /usr/bin/pulseaudio --daemonize=no
           └─31081 /usr/lib/x86_64-linux-gnu/pulse/gsettings-helper

To kill PulseAudio

To stop the PulseAudio process and have it not immediately launch a new instance, use systemctl --user stop:

$ systemctl --user stop pulseaudio.service pulseaudio.socket

To disable PulseAudio

If you do not wish PulseAudio to start automatically upon login, you can disable it like so:

$ systemctl --user disable pulseaudio.service pulseaudio.socket

(By the way, even with it disabled, you can still start PulseAudio when you want it by using systemctl --user start.)


Answering the question in general

The commands above are all you need. However, if you are interested in how to figure out the solution on your own, keep reading.

To find out what processes have the sound card open

Use either the lsof or fuser -v commands to see what processes have the device files open.

$ lsof /dev/snd/*
COMMAND     PID USER      FD   TYPE DEVICE SIZE/OFF  NODE NAME
pulseaudi 32768 hackerb9  3u   CHR  116,5       0t0 15499 /dev/snd/controlC0

To find out what process launched PulseAudio

Use pstree -s to show the parent processes like so:

$ pstree -sp 32768
systemd(1)───systemd(738)───pulseaudio(32768)─┬─gsettings-helpe(32769)─┬─{gsett+
                                              │                        ├─{gsett+
                                              │                        └─{gsett+
                                              └─{pulseaudio}(32773)

I used that to see that PulseAudio was actually getting launched by an instance of systemd (PID 738) which was running in my own account:

$ ps -fp 738
UID        PID  PPID  C STIME TTY          TIME CMD
hackerb9   738     1  0 Feb10 ?        00:00:00 /lib/systemd/systemd --user

From there, it is easy to guess that one needs to use systemctl --user to stop it.


Pulseaudio is a little daemon. man pulseaudio says you can turn it off with the command pulseaudio --kill but doing that takes the rebirth again-- it respawns itself. A normal init script is there to start it at boot, /etc/rc2.d/S50pulseaudio. But when you try controlling this the normal Linux way it doesn't work, because running /etc/init.d/pulseaudio stop doesn't stop it. Removing /etc/rc2.d/S50pulseaudio doesn't prevent it from starting at boot.

To stop its respawning habit, open /etc/pulse/client.conf, change autospawn = yes to autospawn = no, and set daemon-binary to /bin/true. Make sure these lines are uncommented, like this:

 autospawn = no
 daemon-binary = /bin/true

Now we can deal with the normal Linux startup files. First delete /etc/rc2.d/S50pulseaudio. Or you may rename it to a kill command, which preserves the link in case you ever want it again:

$ mv /etc/rc2.d/S50pulseaudio /etc/rc2.d/K50pulseaudio

OR

There may be one more startup file to dispense with: /etc/X11/Xsession.d/70pulseaudio. This starts Pulse when a Gnome session starts. Delete it or copy it to a different directory in case you want to save it, and again verify the exact file name. A useful trick, when you find scripts that start PulseAudio, is to change the binary that they call from /usr/bin/pulseaudio to /bin/true. This is a nice little executable whose only job is to "do nothing, successfully". It keeps the scripts happy, and it's a convenient placeholder if you ever want to change it back.

Now that you have purged all the startup and respawning scripts, Now use the below commands to stop and start the pulse audio.

 $ pulseaudio --kill
 $ pulseaudio --start