3

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.

5
  • A window pop would work, but I supose that's more complex
    – esantix
    Dec 13, 2021 at 15:14
  • 1
    please edit your question and add some example cases. There are things running on your computer all the time. You really don't want a sound every time a command launched by your user finishes, that would be a constant noise. So you need to choose what kind of commands should have the sound. Consider commands like cd, echo, ls, presumably you don't want a noise for those. And then there are the automated things going on. So you really need to narrow this down. Do you maybe want a way to choose to run a command with a beep? Would that do? A function perhaps?
    – terdon on strike
    Dec 13, 2021 at 15:36
  • 1
    It’s not available in many distros, so I’m not adding this as an answer — Fedora has a VTE patch set which adds support for notifications from terminal output, and that’s used to add a system notification when a command completes in a terminal window which is in the background. Dec 13, 2021 at 15:56
  • @terdon, the question is limited to the terminal. The computer runs a lot of processes but they are on the background, not on a terminal.
    – esantix
    Dec 13, 2021 at 17:37
  • 1
    @SantiagoEchevarria typically, I'd say a window notificiation is easier, replace whatever sound-playing command you find in the answers here with 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. Dec 13, 2021 at 18:11

2 Answers 2

2

You 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"
1
  • 1
    I'd recommend adding a check for runtime, i.e. don't play a sound when the last command took less than 20s; that way you don't get notified for every time you type cd, ls or cat on your command line. Dec 13, 2021 at 18:09
1
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;
1
  • I understand I would have to run that each time. I need (would like) to have it automatic. Like somehow automatically changing any_command; to any_command_play_sound; . Maybe something in .bashrc
    – esantix
    Dec 13, 2021 at 15:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .