You are not logged in.

#1 2022-09-05 21:30:10

Gabachin
Member
Registered: 2022-05-06
Posts: 105

Lowering volume before running shutdown service

I configured a shutdown sound on my vanilla Arch setup, by following the wiki and forums:

1). Create shutdownsound.mp3 and place it in  /usr/share/sounds/
2).Create simple shutdownsound.sh in /usr/bin/ and enable permissions. It just has mpv run the mp3.
3).  Create shutdown.service in  /etc/systemd/system

####service
[Unit]
Description=shutdownsound

[Service]
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/usr/bin/shutdownsound.sh

[Install]
WantedBy=multi-user.target

4). enable service: (from /etc/systemd/system, where shutdown.service is); sudo systemctl enable shutdownsound.service

So far, so good. The problem is, sometimes I have the volume very high on shutdown, which makes the ditty very loud. So I inserted

  amixer sset 'Master' 30% && 

into shutdown.sh, and the script runs in the terminal but not on reboot. It does run on reboot with one ampersand, so I assume that the program amixer (same problem occurs with pactl) is killed before the service is applied. Is there any way to fix this?

I tried another approach by creating /etc/rc6.d for reboot and /etc/rc0.d for poweroff and putting the script there, but no luck.

I either shut down my system using rofi which runs a poweroff script, or I shut down or reboot from the command line. I had no luck editing the script by inserting

  amixer sset 'Master' 30% && 

just before the systemctl command that either powers the system off or reboots:

#!/bin/env bash

# Options for powermenu
lock="......Lock"
logout=".....Logout"
shutdown="....Shutdown"
reboot="....Reboot"
sleep="....Sleep"

# Get answer from user via rofi
selected_option=$(echo "$lock
$logout
$sleep
$reboot
$shutdown" | rofi -dmenu\
                  -i\
                  -p "Power"\
                  -config "~/.config/rofi/powermenu.rasi"\
                  -font "Symbols Nerd Font 12"\
                  -width "15"\
                  -lines 5\
                  -line-margin 3\
                  -line-padding 10\
                  -scrollbar-width "0" )

# Do something based on selected option
if [ "$selected_option" == "$lock" ]
then
    /home/$USER/.config/bspwm/scripts/i3lock-fancy/i3lock-fancy.sh
elif [ "$selected_option" == "$logout" ]
then
    bspc quit
elif [ "$selected_option" == "$shutdown" ]
then
    systemctl poweroff
elif [ "$selected_option" == "$reboot" ]
then
    systemctl reboot
elif [ "$selected_option" == "$sleep" ]
then
    amixer set Master mute
    systemctl suspend
else
    echo "No match"
fi

I suppose I coud just create an alias in my fish config to lower the volume then reboot/poweroff, but my interest is piqued now because it seems that this should be an easy thing to accomplish, which it probably is, because I am a total noob.

Offline

#2 2022-09-06 07:01:11

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,483

Re: Lowering volume before running shutdown service

I had no luck editing the script by inserting
  amixer sset 'Master' 30% &&
just before the systemctl command that either powers the system off or reboots

Can you please post the failing script verbatim?`You don't seem to have issues w/ "amixer set Master mute".

Also try

…
amixer sset 'Master' 30%; echo $? >> $HOME/amixer.result # you can read the result of amixer in $HOME/amixer.result after the next boot - hopefully.
systemctl poweroff
…

Offline

#3 2022-09-07 13:38:53

Gabachin
Member
Registered: 2022-05-06
Posts: 105

Re: Lowering volume before running shutdown service

Here is my poweroff script:

#!/usr/bin/env bash

# Manage logout with rofi
option=`echo -e "suspend\nlock screen\nlogout\nreboot\npoweroff\nKill user $USER" | rofi -width 600 -dmenu -p system:`
case $option in
    suspend)
        sudo systemctl  suspend
        ;;
    'lock screen')
        i3lock -i /home/koromicha/Pictures/linuxtux.png
        ;;
    logout)
        i3-nagbar -t warning -m 'Are you sure you  want to exit i3? This will end your X session.' -b 'Yes, exit i3' 'i3-msg exit'
        ;;
    reboot)
       amixer sset 'Master' 30%; echo $? >> $HOME/amixer.result & reboot
        ;;
    poweroff)
        poweroff
        ;;
    'kill user $USER')
        loginctl kill-user $USER
        ;;
esac
fi 

On reboot the file ~/amixer.result returned one line, containing a zero:

 0 

so I guess that means success.

When I created an alias in my fish config,

alias revol='amixer set 'Master' 30%; reboot' 

It executed properly after displaying an output;

 Simple mixer control 'Master',0
  Capabilities: pvolume pswitch pswitch-joined
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 65536
  Mono:
  Front Left: Playback 19661 [30%] [on]
  Front Right: Playback 19661 [30%] [on] 

I think this is perhaps why the command does not run in the .service file.  These aliases work in my fish config:

# reboot or poweroff lower volume for shutdown sound
alias revol='amixer set 'Master' 30% > /dev/null 2>&1; mpv /usr/share/sounds/shutdown.mp3  > /dev/null 2>&1; reboot'
alias powvol='amixer set 'Master' 30% > /dev/null 2>&1; mpv /usr/share/sounds/shutdown.mp3  > /dev/null 2>&1; poweroff'

but I would really prefer doing this with a service so the operation is automatic.

Last edited by Gabachin (2022-09-07 15:01:49)

Offline

#4 2022-09-07 14:20:01

Maniaxx
Member
Registered: 2014-05-14
Posts: 761

Re: Lowering volume before running shutdown service

I would rather use 'alsactl restore -f myalsa.state' to reset all values.
Instead of forging a custom service file you could place your script into '/usr/lib/systemd/system-shutdown' (unless it kicks in too late).

Immediately before executing the actual system halt/poweroff/reboot/kexec systemd-shutdown will run all executables in /usr/lib/systemd/system-shutdown/
https://man.archlinux.org/man/systemd-shutdown.8.en

Last edited by Maniaxx (2022-09-07 14:24:27)


sys2064

Offline

#5 2022-09-07 14:31:17

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,483

Re: Lowering volume before running shutdown service

amixer sset 'Master' 30%; echo $? >> $HOME/amixer.result & reboot

Why would you ever fork that?

amixer sset 'Master' 30%; echo $? >> $HOME/amixer.result; reboot

Offline

#6 2022-09-07 15:22:41

Gabachin
Member
Registered: 2022-05-06
Posts: 105

Re: Lowering volume before running shutdown service

@Maniaxx: Yes, I tried that with no success. I'm a noob but I do RTFM lol. It just seems odd to me that this setup does not work:

####service
[Unit]
Description=shutdownsound

[Service]
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/usr/bin/shutdown.sh

[Install]
WantedBy=multi-user.target
/usr/bin/shutdown.sh

#! /usr/bin/bash

amixer set 'Master' 30% > /dev/null 2>&1; mpv /usr/share/sounds/shutdown.mp3  > /dev/null 2>&1 

Offline

#7 2022-09-07 15:37:37

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,483

Re: Lowering volume before running shutdown service

"does not work" how?

#!/bin/bash
date >> >> /var/log/shutdown.log
amixer set 'Master' 30% >> /var/log/shutdown.log 2>&1 
mpv /usr/share/sounds/shutdown.mp3 >> /var/log/shutdown.log 2>&1

Does it work™ when you isolate the rescue-target (ie. stop the multi-user.target, 2nd link below)?

Offline

#8 2022-09-07 16:00:43

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 25,263

Re: Lowering volume before running shutdown service

I'd use a Type=oneshot service so that systemd doesn't think in can just mulitthread the execution away.

[Unit]
Description=shutdownsound

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/usr/bin/shutdown.sh

[Install]
WantedBy=multi-user.target sound.target

maybe also leverage the sound target so this is also executed before the sound devices go away

Offline

#9 2022-09-07 19:11:04

Maniaxx
Member
Registered: 2014-05-14
Posts: 761

Re: Lowering volume before running shutdown service

'WantedBy=graphical-session.target' could be a good candidate as well.


sys2064

Offline

#10 2022-09-08 01:56:38

Gabachin
Member
Registered: 2022-05-06
Posts: 105

Re: Lowering volume before running shutdown service

@Seth @V1Del: running the script and checking the log, I got

Wed Sep  7 08:42:42 PM CDT 2022
Simple mixer control 'Master',0
  Capabilities: pvolume pvolume-joined pswitch pswitch-joined
  Playback channels: Mono
  Limits: Playback 0 - 87
  Mono: Playback 26 [30%] [-45.75dB] [on]
[ffmpeg/demuxer] mp3: Estimating duration from bitrate, this may be inaccurate
 (+) Audio --aid=1 (mp3 2ch 22050Hz)
File tags:
 Artist: DTOS Sounds
 Album: DTOS Sounds
 Comment: excellent!
 Date: 2022
 Genre: Classical
 Title: shutdown-01
 Track: 01/16
AO: [alsa] 48000Hz stereo 2ch float
A: 00:00:05 / 00:00:05 (95%)

Exiting... (End of file)

which is exactly what I get when I run my shutdown script in the terminal. When I change the type to 'oneshot' and 'WantedBy=multi-user.target' to 'WantedBy=multi-user.target sound.target' it "works" on my Dell XPS 13 i5 6th, vanilla arch, kernel 5.19.7.zen2-1-zen, i3wm, but not on my Dell Vostro i7 10th, same OS configuration. The problem here, of course, is that I do not know what most of these settings are actually doing. I can follow the wiki, and the suggestions on the forums, but that's it. I don't really care too much about the shutdown sound per se, my goal is to learn. Got the linux bug six months or so ago. Can you explain what your suggested modifications are intended to do. And what "maybe also leverage the sound target so this is also executed before the sound devices go away" means? I think that since the script executes in the terminal but not in the .service, that the programs amixer and pactl get killed before the system tries to execute the service because if I use & the shutdown sound is heard but not at the lowered volume but if I use && then .service is not executed  at all because in this case poweroff occurs almost instantly.

Last edited by Gabachin (2022-09-08 02:08:58)

Offline

#11 2022-09-08 07:11:46

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,483

Re: Lowering volume before running shutdown service

programs amixer and pactl get killed before the system tries to execute the service

Those are executed *by* the service.

"leverage the sound target" is the additional "sound.target" in WantedBy

"works" … but not on my Dell Vostro i7

And what does the log say there?

Offline

#12 2022-09-19 15:08:06

Gabachin
Member
Registered: 2022-05-06
Posts: 105

Re: Lowering volume before running shutdown service

It turns out that none of the suggestions is working for me. Here is a summary of what is happening: when I run either (1). 

 amixer sset 'Master' 30% & mpv /usr/share/sounds/shutdown.mp3

or (2). 

 amixer sset 'Master' 30% && mpv /usr/share/sounds/shutdown.mp3

in the terminal, the volume reduces to 30%, the shutdown sound is heard, and the system reboots. When I put command (1) in a script and incorporate it in the service, the shutdown sound is heard at full volume and the system shuts down. When I put command (2) in the service, no sound is heard and the system shutsdown. So my assumption is that there is a problem with the command

amixer sset 'Master' 30%

I do not understand where or even why there would be a problem running a bash script in the body of

[Unit]
Description=shutdownsound

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/usr/bin/shutdown.sh

[Install]
WantedBy=multi-user.target sound.target

Last edited by Gabachin (2022-09-19 15:09:09)

Offline

#13 2022-09-19 15:12:15

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,483

Re: Lowering volume before running shutdown service

Either the sound is played too late or not at all (because amixer fails)
What happens for

 amixer sset 'Master' '30%' ; mpv /usr/share/sounds/shutdown.mp3

Offline

#14 2022-10-03 05:35:11

Gabachin
Member
Registered: 2022-05-06
Posts: 105

Re: Lowering volume before running shutdown service

Success. Many thanks. But now I am confused as to the difference between ; and & . The semicolon means don't check the exit code of the previous command, just execute the next one, whereas && says chain the commands as if they were one command, which means you get one exit code, so that if any command in the chain fails, the whole thing fails. So far so good. But doesn't & have the same effect as ; ? The whole point of this exercise is to learn. I am not really concerned with the sounds themselves.

Last edited by Gabachin (2022-10-03 05:36:36)

Offline

#15 2022-10-03 06:04:22

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,483

Re: Lowering volume before running shutdown service

"&" forks the previous command, ie. the shell doesn't wait for it to finish before executing the next one

(sleep 2; echo foo); echo bar
(sleep 2; echo foo) & echo bar

The braces "()" create a subshell to group commands.

https://tldp.org/LDP/Bash-Beginners-Guide/html/

Please always remember to mark resolved threads by editing your initial posts subject - so others will know that there's no task left, but maybe a solution to find.
Thanks.

Offline

#16 2022-10-05 13:22:45

Maniaxx
Member
Registered: 2014-05-14
Posts: 761

Re: Lowering volume before running shutdown service

Gabachin wrote:

whereas && says chain the commands as if they were one command, which means you get one exit code, so that if any command in the chain fails, the whole thing fails.

&& is a conditional 'logical AND' (arithmetic operation) of the exit code.
https://stackoverflow.com/questions/451 … ll-command


sys2064

Offline

Board footer

Powered by FluxBB