Need to restart sound service after suspend/hibernate after installing NVIDIA proprietary driver

I have a Dell Optiplex 780 that I equipped with a NVIDIA GT 1030 video card. After installing the NVIDIA proprietary driver version 535.146.02 I noticed that pulseaudio does not work after resuming from suspend or hibernate. This is in particular true when trying to stream sound from firefox. I have found that I can get pulseaudio functional again with:

systemctl restart sound.target

However, this requires root privileges. I wonder whether there is a config file where I can add the line above so that it is read upon resuming from suspend or hibernate? If there are any other suggested solutions how to resolve the pulseaudio issue upon resuming from suspend/hibernate I would appreciate to know.

Thanks!

In /usr/lib/systemd/system I have:

ls -t *suspend* *hibernate* *resume*
nvidia-hibernate.service
nvidia-resume.service
nvidia-suspend.service
systemd-suspend.service
systemd-hibernate-resume@.service
systemd-hibernate-resume@.service
systemd-hibernate.service
systemd-suspend-then-hibernate.service
systemd-suspend-then-hibernate.service
hibernate.target
suspend-then-hibernate.target
suspend-then-hibernate.target
suspend.target

There is also a related script:

/usr/bin/nvidia-sleep.sh

Is any of these a good place where to place commands to be executed or services to be restarted when resuming from suspend or hibernate?

Thanks!

@gianluca44 check the output from journalctl to see what is run when it comes out of suspend…

This is what I think is relevant:

Dec 09 19:24:11 venere systemd-sleep[29248]: INFO: Skip running /usr/lib/systemd/system-sleep/grub2.sleep for suspend
Dec 09 19:24:06 venere suspend[29257]: nvidia-resume.service
Dec 09 19:24:05 venere systemd-sleep[29187]: System returned from sleep state.
Dec 09 19:24:11 venere logger[29257]: <13>Dec 9 19:24:06 suspend: nvidia-resume.service
Dec 09 19:24:06 venere systemd[1]: nvidia-resume.service: Deactivated successfully.
Dec 09 19:24:06 venere systemd[1]: Finished NVIDIA system resume actions.

@gianluca44 if you copy /usr/lib/systemd/system/nvidia-resume.service to /etc/systemd/system/nvidia-resume.service and edit the file in the [Service] to include;

ExecStartPost=+/usr/bin/systemctl restart sound.target

Then run systemctl daemon-reload suspend/hibernate, then resume and see if all good…

Maybe I should create a script:

/usr/lib/systemd/system-sleep/restart-sound-resume

#!/bin/sh

case “$1” in
post)
systemctl restart sound.target
;;
esac

I copied this from cat /usr/lib/systemd/system-sleep/nvidia

#!/bin/sh

case “$1” in
post)
/usr/bin/nvidia-sleep.sh “resume”
;;
esac

@gianluca44 try the systemd way first…

Dean Martin posted: pm-utils stops working - #8 by deano_ferrari

Replace

ExecStart=/usr/bin/hdparm -S 60 /dev/sdb

by

ExecStart=/usr/bin/systemctl restart sound.target

@malcolmlewis Sorry I had missed your post above. Just want to make sure I do it correctly:

  1. Why do I need a “+” in “ExecStartPost=+/usr/bin/systemctl restart sound.target”?

  2. If I copy /usr/lib/systemd/system/nvidia-resume.service to /etc/systemd/system/nvidia-resume.service don’t I end up with two versions of the same file? I mean, are both files going to be executed?

  3. Are you suggesting the file /etc/systemd/system/nvidia-resume.service should then look like this:

[Unit]
Description=NVIDIA system resume actions
After=systemd-suspend.service
After=systemd-hibernate.service

[Service]
Type=oneshot
ExecStart=/usr/bin/logger -t suspend -s "nvidia-resume.service"
ExecStart=/usr/bin/nvidia-sleep.sh "resume"
ExecStartPost=+/usr/bin/systemctl restart sound.target

[Install]
WantedBy=systemd-suspend.service
WantedBy=systemd-hibernate.service

Right now, the file /usr/lib/systemd/system/nvidia-resume.service looks the same as above but without the line with ExecStartPost.

Thanks for your help!

According to the documentation:

If the executable path is prefixed with “+” then the process is executed with full privileges. In this mode privilege restrictions configured with User=, Group=, CapabilityBoundingSet= or the various file system namespacing options (such as PrivateDevices=, PrivateTmp=) are not applied to the invoked command line (but still affect any other ExecStart=, ExecStop=, … lines). However, note that this will not bypass options that apply to the whole control group, such as DevicePolicy=, see systemd.resource-control(5) for the full list.

Files in /etc override any system defaults plus on an update your changes don’t get replaced any changes by the system admin get done in / etc for this reason.

@malcolmlewis Oh, I see. So I just copy the file to /etc and add the line as you suggested?

A full copy of the service file will override any newer vendor version of the service, thus disabling all updates to the service. You may want to create a drop-in by running systemctl edit nvidia-resume.service:

erlangen:~ # systemd-delta --type=extended /etc/systemd/system
[EXTENDED]   /etc/systemd/system/failure-notification@.service → /etc/systemd/system/failure-notification@.service.d/toplevel-override.conf
[EXTENDED]   /usr/lib/systemd/system/btrfs-balance.timer → /etc/systemd/system/btrfs-balance.timer.d/schedule.conf
[EXTENDED]   /usr/lib/systemd/system/btrfs-defrag.timer → /etc/systemd/system/btrfs-defrag.timer.d/schedule.conf
[EXTENDED]   /usr/lib/systemd/system/btrfs-scrub.timer → /etc/systemd/system/btrfs-scrub.timer.d/schedule.conf
[EXTENDED]   /usr/lib/systemd/system/btrfs-trim.timer → /etc/systemd/system/btrfs-trim.timer.d/schedule.conf
[EXTENDED]   /usr/lib/systemd/system/fetchmail.service → /etc/systemd/system/fetchmail.service.d/override.conf
[EXTENDED]   /usr/lib/systemd/system/minidlna.service → /etc/systemd/system/minidlna.service.d/override.conf

7 overridden configuration files found.
erlangen:~ # 

https://www.freedesktop.org/software/systemd/man/latest/systemd.unit.html#id-1.14.3

I tried the suggestion by @malcolmlewis. It did not work at first even after systemctl daemon-reload. So I rebooted. Sound works only the after the first time that I hibernate. If I hibernate a second time and resume, then the sound does not work, unless I restart the sound.target manually. I may have to try a different solution.

I also tried my own suggestion above and created the file /usr/lib/systemd/system-sleep/restart-sound-resume

#!/bin/sh

case “$1” in
post)
/usr/bin/systemctl restart sound.target
;;
esac

That didn’t solve it either. Is it safe to add something like sleep 5 before restarting the sound.target?

Please, always use the </> button and not the " button when posting code/computer text.
It now looks as if the quotes around $1 are of the wrong type. But that could be something the forum’s software is doing to “normal” text.

sleep 5 didn’t solve it either.

One interesting thing is that if I launch vlc or mplayer -ao pulse and play a short video/audio file, then sound can be streamed from firefox. It seems that using vlc or mplayer -ao pulse unlocks something and then firefox works correctly with pulseaudio.

I may try to write a small script that plays sound for very short (maybe with volume off) and then have a wrapper for hibernate/suspend.

PS: I did try earlier by restarting pulseaudio as user but that didn’t work either.