You are not logged in.

#1 2016-10-22 08:21:54

harshad1
Member
Registered: 2013-09-02
Posts: 20

Cannot control Pulseaudio from a systemd unit

I would really like my audio to mute before my laptop goes to sleep. In order to do this I wrote the following python script:

import re
import subprocess
import getpass
print("Current user = " + getpass.getuser())

sink_dump = subprocess.check_output(["pacmd","list-sinks"])
sink_dump_string = sink_dump.decode("utf-8")
myre = re.compile(r"\s*index:\s*([0-9]+)",re.MULTILINE)
sinks = myre.findall(sink_dump_string)
for sink in sinks:
    subprocess.call(["pacmd","set-sink-mute",sink,"1"])

This script works great when run as <my username>.

In order to get this to run at sleep I wrote the following unit:

[Unit]
Description='Muting sound on sleep'
Before=suspend.target

[Service]
User=<my username> 
Type=oneshot
RemainAfterExit=no
ExecStart=-/usr/bin/python /path/to/script.py

[Install]
WantedBy=suspend.target

This however fails with an error of


> No PulseAudio daemon running, or not running as session daemon.
> Current user = <my username>

You'll note that the script is indeed being run as <my username> per getpass.getuser().

I don't understand why this is happening. Could anyone point me in the right direction?

Thank you

Offline

#2 2016-10-22 08:27:09

harshad1
Member
Registered: 2013-09-02
Posts: 20

Re: Cannot control Pulseaudio from a systemd unit

Sorry. Just figured this out (not 30 seconds after posting this)

Per https://wiki.archlinux.org/index.php/Pu … 2C_cron.29

I needed to add

Environment=XDG_RUNTIME_DIR=/run/user/1000 under [Service]

[Thanks smile ]

Offline

Board footer

Powered by FluxBB