关机时运行 sh 脚本来降低音量

关机时运行 sh 脚本来降低音量

在新笔记本电脑上设置好几乎所有我想要的东西后,仍然有一件小事我无法完成,这真的让我很烦恼:我想在关机时运行一个脚本,这样启动/重启时的音量几乎是静音的。以下是我所做的:

我有一个像这样的 sh 脚本

  #!/bin/sh

  amixer -D pulse sset Master 20%

放置在 /etc/init.d/K99_set_volume.sh 中并已使用

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

建立正确的链接。不幸的是,启动时的音量与上一个会话的音量相同。我尝试在降低音量后明确在脚本中插入一行“alsactl store”,但没有效果。该脚本是可执行的,在终端中运行时可以工作,并且音量确实已设置/var/lib/alsa/asound.state。此外,脚本在关机时运行,我检查过。不过,我不知道数字 99 是否正确。

我知道有一些关于该主题的帖子,但我真的很想找到一个在关机/启动时使用脚本的解决方案,而且我读过的帖子建议我做的事情与我做的差不多?

pacmd list-sinks 的完整输出是

 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  

答案1

问题是,pulseaudio 是设置音量的那个,而不是 ALSA。如果你真的想修改音量,我建议你改用pactl

pactl set-sink-volume 0 20%

并且只在启动时执行此操作。我更希望您等到 pulseaudio 启动。为此最好使用 upstart,因为它是基于事件的初始化。为此,只需setvolume.conf在目录中创建文件/etc/init,并用以下内容填充它:

start on started pulseaudio

setgid username
setuid username

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

这将确保 pulseaudio 启动后将具有用户所需的音量。

答案2

经过几个小时的试验,阅读了许多笔记,我找到了解决方案的一部分本网站。所以,我知道这个简单的任务不需要脚本,而且我也知道脚本systemctl --user就足够了。测试平台:Ubuntu 18.04.4 LTS

设置关机和重启时的音量
设备:pacmd list-sinks
用途:pactl info

man pactl  
man systemd  

pactl- 控制正在运行的 PulseAudio 声音服务器,
pactl可用于向 PulseAudio 声音服务器发出控制命令。

测试在终端:

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

在你的主目录中创建文件: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

测试:

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:

故障排除:

journalctl -xe
systemctl --user status setvolume.service

相关内容