You are not logged in.
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.target4). 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"
fiI 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
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
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
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
amixer sset 'Master' 30%; echo $? >> $HOME/amixer.result & rebootWhy would you ever fork that?
amixer sset 'Master' 30%; echo $? >> $HOME/amixer.result; rebootOffline
@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
"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>&1Does it work™ when you isolate the rescue-target (ie. stop the multi-user.target, 2nd link below)?
Offline
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.targetmaybe also leverage the sound target so this is also executed before the sound devices go away
Offline
'WantedBy=graphical-session.target' could be a good candidate as well.
sys2064
Offline
@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
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
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.mp3or (2).
amixer sset 'Master' 30% && mpv /usr/share/sounds/shutdown.mp3in 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.targetLast edited by Gabachin (2022-09-19 15:09:09)
Offline
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.mp3Offline
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
"&" 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 barThe 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
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