What's an efficient way to get the terminal to play an alarm/sound when a command launched by user finishes?
It needs to be automatic so that I don't have to explicitly run a "beep" command after each command.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityYou can use beep, echo -en "\007"
it is very basic thing which use speaker on MB, you do non need even sound card. But in now days it is often disabled.
You can try this to enable it https://superuser.com/a/22769
In many distributions, you have PulseAudio, so you can play sound from CLI like this paplay /usr/share/sounds/freedesktop/stereo/complete.oga
Some other ways can be founded on https://askubuntu.com/questions/277215/how-to-make-a-sound-once-a-process-is-complete
[EDIT]
Sorry, I didn't notice yours on every command edition.
You can probably combine what is written before with trap
to accomplish required behavior
https://jichu4n.com/posts/debug-trap-and-prompt_command-in-bash/
# This will run before any command is executed.
function PreCommand() {
if [ -z "$AT_PROMPT" ]; then
return
fi
unset AT_PROMPT
}
trap "PreCommand" DEBUG
# This will run after the execution of the previous full command line. We don't
# want it PostCommand to execute when first starting a bash session (i.e., at
# the first prompt).
FIRST_PROMPT=1
function PostCommand() {
AT_PROMPT=1
if [ -n "$FIRST_PROMPT" ]; then
unset FIRST_PROMPT
return
fi
paplay /usr/share/sounds/freedesktop/stereo/complete.oga
}
PROMPT_COMMAND="PostCommand"
cd
, ls
or cat
on your command line.
Dec 13, 2021 at 18:09
play_sound(){
local df="./somedirectory/music_file.wav";
$(which cvlc) \
-q \
--play-and-exit \
--gain=0.5 \
${df} \
&>/dev/null \
;
};
some_command;
play_sound;
You have to install command line vlc for it to work.
It's called cvlc.
sudo apt-get install cvlc;
notify-send -u critical '<b>fail 🐋</b><br />This did not succeed.'
, or whatever you want to display! Also, things like gnome-terminal have automatical notifications when the state of a terminal changes, which you need to do nothing special for but activate it.