4

After having set up almost everything I wanted on my new laptop, there is still this one little thing that I cannot get to work and that's really annoying me: I would like to run a script at shut down such that the volume at boot/reboot is almost silent. Here is what I've done:

I have a sh script looking like this

  #!/bin/sh

  amixer -D pulse sset Master 20%

placed in /etc/init.d/K99_set_volume.sh and have used

ln -s /etc/init.d/K99_set_volume.sh /etc/rc0.d/K99_set_volume.sh
ln -s /etc/init.d/K99_set_volume.sh /etc/rc6.d/K99_set_volume.sh

to make the right links. Unfortunately, the volume at boot is the same level as the previous session. I tried to explicitly insert a line with "alsactl store" into the script after reducing the volume, but to no effect. The script is executable and works when run in a terminal and the volume is indeed set in /var/lib/alsa/asound.state. Also, the script runs at shut down, I checked that. I don't know if the number 99 is right, though.

I know there are some posts on that topic, but I would really like to find a solution with a script at shut down/boot and the posts I've read suggest to do pretty much what I do?

The full output from pacmd list-sinks is

 2 sink(s) available.  
    index: 0    
    name: alsa_output.pci-0000_00_03.0.hdmi-stereo   
    driver: module-alsa-card.c  
    flags: HARDWARE DECIBEL_VOLUME LATENCY DYNAMIC_LATENCY  
    state: SUSPENDED  
    suspend cause: IDLE  
    priority: 9950  
    volume: 0:  20% 1:  20%  
            0: -41,94 dB 1: -41,94 dB  
            balance 0,00  
    base volume: 100%  
                 0,00 dB  
    volume steps: 65537  
    muted: yes  
    current latency: 0,00 ms  
    max request: 0 KiB  
    max rewind: 0 KiB  
    monitor source: 0  
    sample spec: s16le 2ch 44100Hz  
    channel map: front-left,front-right  
                 Stereo  
    used by: 0  
    linked by: 0  
    configured latency: 0,00 ms; range is 0,50 .. 371,52 ms  
    card: 0 alsa_card.pci-0000_00_03.0  
    module: 5  
    properties:  
        alsa.resolution_bits = "16"  
        device.api = "alsa"  
        device.class = "sound"  
        alsa.class = "generic"  
        alsa.subclass = "generic-mix"  
        alsa.name = "HDMI 0"  
        alsa.id = "HDMI 0"  
        alsa.subdevice = "0"  
        alsa.subdevice_name = "subdevice #0"  
        alsa.device = "3"  
        alsa.card = "0"  
        alsa.card_name = "HDA Intel HDMI"  
        alsa.long_card_name = "HDA Intel HDMI at 0xf7d1c000 irq 61"  
        alsa.driver_name = "snd_hda_intel"  
        device.bus_path = "pci-0000:00:03.0"  
        sysfs.path = "/devices/pci0000:00/0000:00:03.0/sound/card0"  
        device.bus = "pci"  
        device.vendor.id = "8086"  
        device.vendor.name = "Intel Corporation"  
        device.product.id = "0a0c"  
        device.form_factor = "internal"  
        device.string = "hdmi:0"  
        device.buffering.buffer_size = "65536"  
        device.buffering.fragment_size = "32768"  
        device.access_mode = "mmap+timer"  
        device.profile.name = "hdmi-stereo"  
        device.profile.description = "Digital Stereo (HDMI)"  
        device.description = "Built-in Audio Digital Stereo (HDMI)"  
        alsa.mixer_name = "Intel Haswell HDMI"  
        alsa.components = "HDA:80862807,80860101,00100000"  
        module-udev-detect.discovered = "1"  
        device.icon_name = "audio-card-pci"  
    ports:  
        hdmi-output-0: HDMI / DisplayPort (priority 5900, latency offset 0 usec, available: no)  
            properties:  
                device.icon_name = "video-display"  
    active port: hdmi-output-0  
    index: 1  
    name: alsa_output.pci-0000_00_1b.0.analog-stereo  
    driver: module-alsa-card.c  
    flags: HARDWARE HW_MUTE_CTRL HW_VOLUME_CTRL DECIBEL_VOLUME LATENCY
    state: SUSPENDED  
    suspend cause: IDLE  
    priority: 9959  
    volume: 0:  20% 1:  20%  
            0: -41,94 dB 1: -41,94 dB  
            balance 0,00  
    base volume: 100%  
                 0,00 dB  
    volume steps: 65537  
    muted: no  
    current latency: 0,00 ms  
    max request: 0 KiB  
    max rewind: 0 KiB  
    monitor source: 1  
    sample spec: s16le 2ch 44100Hz  
    channel map: front-left,front-right  
                 Stereo  
    used by: 0  
    linked by: 0  
    configured latency: 0,00 ms; range is 0,50 .. 371,52 ms  
    card: 1 alsa_card.pci-0000_00_1b.0  
    module: 6  
    properties:  
        alsa.resolution_bits = "16"  
        device.api = "alsa"  
        device.class = "sound"  
        alsa.class = "generic"  
        alsa.subclass = "generic-mix"  
        alsa.name = "CX20751/2 Analog"  
        alsa.id = "CX20751/2 Analog"  
        alsa.subdevice = "0"  
        alsa.subdevice_name = "subdevice #0"  
        alsa.device = "0"  
        alsa.card = "1"  
        alsa.card_name = "HDA Intel PCH"  
        alsa.long_card_name = "HDA Intel PCH at 0xf7d18000 irq 60"  
        alsa.driver_name = "snd_hda_intel"  
        device.bus_path = "pci-0000:00:1b.0"  
        sysfs.path = "/devices/pci0000:00/0000:00:1b.0/sound/card1"  
        device.bus = "pci"  
        device.vendor.id = "8086"  
        device.vendor.name = "Intel Corporation"  
        device.product.id = "9c20"  
        device.product.name = "Lynx Point-LP HD Audio Controller"  
        device.form_factor = "internal"  
        device.string = "front:1"  
        device.buffering.buffer_size = "65536"  
        device.buffering.fragment_size = "32768"  
        device.access_mode = "mmap+timer"  
        device.profile.name = "analog-stereo"  
        device.profile.description = "Analog Stereo"  
        device.description = "Built-in Audio Analog Stereo"  
        alsa.mixer_name = "Conexant CX20751/2"  
        alsa.components = "HDA:14f1510f,104315dd,00100100"  
        module-udev-detect.discovered = "1"  
        device.icon_name = "audio-card-pci"  
    ports:  
        analog-output-speaker: Speakers (priority 10000, latency offset 0 usec, available: unknown)  
            properties:  
                device.icon_name = "audio-speakers"  
        analog-output-headphones: Headphones (priority 9000, latency offset 0 usec, available: no)  
            properties:  
                device.icon_name = "audio-headphones"  
    active port: analog-output-speaker  
TuKsn
  • 4,370
  • 2
  • 26
  • 43

2 Answers2

0

After experimenting for several hours, reading many notes I found a part of the solution on this website. So, I knew no script is needed for this simple task and also I learned the systemctl --user is enough. Tested on: Ubuntu 18.04.4 LTS

Set Volume at shut down & reboot
Devices: pacmd list-sinks
Usage: pactl info

man pactl  
man systemd  

pactl - Control a running PulseAudio sound server
pactl can be used to issue control commands to the PulseAudio sound server.

Test in terminal:

pactl set-sink-volume 0 20%  
pactl set-sink-volume 0 80%

create the file in your home directory: nano ~/.config/systemd/user/setvolume.service

[Unit]
Description=Set the volume at shut down and reboot to desired value

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/pactl set-sink-volume 0 40%    

[Install]
WantedBy=default.target

Testing:

systemctl --user daemon-reload
systemctl --user stop setvolume
systemctl --user start setvolume
systemctl --user restart setvolume
systemctl --user enable setvolume   enable and start the systemd user service:

Troubleshooting:

journalctl -xe
systemctl --user status setvolume.service
Tejas Lotlikar
  • 2,945
  • 5
  • 17
  • 26
tido
  • 1
0

The issue is that is pulseaudio the one setting the volume, not ALSA. If you really like to modify the volume, I recommend you to instead use pactl:

pactl set-sink-volume 0 20%

and do it only at boot time. I would prefer that you waits until pulseaudio is started. For that is better using upstart, since it's an event based init. For this, just create the setvolume.conf file in the /etc/init directory, and fill it with the following content:

start on started pulseaudio

setgid username
setuid username

script
    exec pactl set-sink-volume 0 20%
end script

This will assure you that after pulseaudio starts it will be with the desired volume for your user.

Braiam
  • 67,791
  • 32
  • 179
  • 269
  • Thanks! Unfortunately, that didn't work either. I should maybe mention that I have trusty tahr installed (don't know exactly if anything changed) and the command " pactl set-sink-volume 0 20% " does not work when executed in terminal. – user285502 May 26 '14 at 22:24
  • @user285502 that's odd, edit your question and include pacmd list-sinks – Braiam May 26 '14 at 22:35
  • Ok, in a terminal, pactl set-sink-volume 1 20% works. I tried to change the sink in the boot script but that didn't work. – user285502 May 27 '14 at 07:46
  • @user285502 can you do touch /home/user/test in the exec stanza? – Braiam May 27 '14 at 14:28
  • No, no file is created. I tried to run the script without the condition that pulseaudio is started but it still doesn't create the file. – user285502 May 27 '14 at 18:40
  • @user285502 now you are scaring me... – Braiam May 27 '14 at 22:55