How can I play a sound when script execution is ready?

I am executing every now and then some python scripts which take quite long to execute.

I execute them like this:
$ time python MyScript.py

How can I play a sound as soon as the execution of the script is done?

I use Ubuntu 10.10 (Gnome desktop).

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Append any command that plays a sound; this could be as simple as

$ time mycommand; printf '7'

or as complex as

$ time mycommand && paplay itworked.ogg || paplay bombed.ogg

(Commands assume pulseaudio is installed; substitute your sound player, which will depend on your desktop environment.)

Method 2

spd-say

sleep 2; spd-say 'get back to work'

Infinite loop with -w if you need extra motivation:

sleep 2; while true; do spd-say -w 'get back to work'; done

or if you prefer the carrot:

sleep 2; while true; do spd-say -t female1 -w "I'm done, come back to me, darling"; done

Pre-installed on 14.04 via package speech-dispatcher: http://releases.ubuntu.com/trusty/ubuntu-14.04.4-desktop-amd64.manifest for blind people I suppose?

Also add a popup

This combo is a life saver (b stands for beep):

b() ( spd-say 'done'; zenity --info --text "$(date);$(pwd)" & )

and then:

super-slow-command;b

If I’m somewhere in the room, I’ll hear it and know that the long job is done.

Otherwise, I’ll see the popup when I get back to my computer.

Related: https://stackoverflow.com/questions/7035/how-to-show-a-gui-message-box-from-a-bash-script-in-linux

Method 3

Just pick a sound on your hard drive, and put a command to play it right after the command you’re waiting on; they’ll happen sequentially:

$ time python MyScript.py; mplayer ~/ScriptDone.wav

(You can use any player, naturally). I have a script called alertdone that plays a tone and shows an libnotify alert when run; I use it for exactly this occasion:

$ time python MyScript.py; alertdone "Done timing"

It’s really simple, so if you want to make your own you can base it on this (mine requires notify-more, mplayer, and ~/tones/alert_1.wav though):

#!/bin/bash
message=${1:-"Finished working"}
notify-more -t 10000 -i /usr/share/icons/gnome/32x32/actions/insert-object.png "Process Finished" "$message"
mplayer ~/tones/alert_1.wav

Method 4

time python MyScript.py; play /path/so/sound.ogg

play is a very basic (no UI) sound player from the sox Install sox http://bit.ly/software-small package. You can replace it by any other command-line-driven sound player.

Method 5

Personally, I use my-script && notify-send "done". This sends a desktop notification, which on Linux Mint(Cinnamon) looks like this:

enter image description here

Method 6

You can also make this happen automatically.

I will show you how in zsh, then add info about bash.

The essence looks like this:

preexec()
{
    starttime=$SECONDS
}

precmd()
{
    if ((SECONDS - starttime >= 5)); then
        aplay "sound.wav"
        # or printf "b", or notify-send, or whatever
    fi
}

You can also make it only do it if the program was Python, e.g.

preexec()
{
    starttime=$SECONDS
    case $3 in python*)
        command_is_python=true;;
    *)
        command_is_python=false;;
    esac
}

precmd()
{
    if $command_is_python && ((SECONDS - starttime >= 5)); then
        aplay "sound.wav"
        # or printf "b", or notify-send, or whatever
    fi
}

In bash, the best way is to download preexec.bash.txt and source it (e.g. . ~/preexec.bash.txt at the top of your ~/.bashrc, then the above (or something close to it) should work. (Not sure about the $3 bit to check if the command is Python.)

If you’re using GNOME Terminal, I would also point you to Flashing GNOME Terminal. It’s a patch I wrote that makes the terminal blink when a command is done, so you can Alt-Tab to something else, then it lets you know when it’s done.

Method 7

You don’t need to add a command to everything, you can actually use a script, that does this automatically for you. It is called undistract-me and it is available on Github.

example

sudo apt install undistract-me    #installs the script (on Debian)
echo 'source /etc/profile.d/undistract-me.sh' >> ~/.bashrc #adds auto-enable to your console
echo 'export LONG_RUNNING_COMMAND_TIMEOUT=XXX' >> ~/.bashrc #where XXX is number of seconds when the command is long enough to alert you
echo 'export UDM_PLAY_SOUND=1' >> ~/.bashrc #to enable sound alert

now start new bash and you are set. Sound and alert can be changed by modifying the script.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x