Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 538  / 1 Year ago, sun, may 14, 2023, 8:46:15

I find it annoying when I forget to stop videos before closing my laptop and then having sound playing when opening it in public.



I tried to add the following script to /lib/systemd/system-sleep:



#!/bin/bash

amixer -q -D pulse sset Master mute

echo 2 >> /home/user/test


The script seems to be called when suspending, as new lines are being appended to the test file (twice, which might indicate that the script also runs on resume - can someone confirm this assumption?).



Either the sound system re-enables itself automatically or something else is wrong. But the amixer command, when executed manually, works fine.



Any ideas?


More From » bash

 Answers
7

You can mute the sound before suspend by creating a systemd service.




  • First, make sure that your script is executable:



    chmod u+x /path/to/your/script.sh

  • Then, create a systemd service that runs your script, by running:



    sudo nano /etc/systemd/system/mute_before_sleep.service

  • In the nano window enter the following:



    [Unit]
    Description=Mute sound before suspend
    Before=suspend.target

    [Service]
    ExecStart=/path/to/your/script.sh
    User=<your_username>
    Environment=DISPLAY=:0

    [Install]
    WantedBy=suspend.target


    In ExecStart= enter the path to your script and in User= enter your username.


  • Save and close nano by pressing Ctrl+O and then Ctrl+X.


  • Finally, enable the service:



    sudo systemctl enable mute_before_sleep.service


[#4332] Sunday, May 14, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
itagde

Total Points: 241
Total Questions: 113
Total Answers: 118

Location: Liechtenstein
Member since Wed, Dec 8, 2021
2 Years ago
;