Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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).

share|improve this question
    
Are you using bash or zsh? There are actually some ways of making this happen automatically, but they're quite involved and depend on which shell you are using. –  Mikel Mar 4 '11 at 23:06
    
$ echo $SHELL returns "/bin/bash" –  moose Mar 5 '11 at 20:39

4 Answers 4

up vote 14 down vote accepted

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.)

share|improve this answer
3  
+1 paplay is probably the best command to use to play a sound on a "modern" Linux system. –  Mikel Mar 4 '11 at 23:21
2  
printf '\7' doesn't work for me, but this command worked as I wanted it time python MyScript.py -n 40 && paplay /usr/share/sounds/ubuntu/stereo/desktop-login.ogg || paplay /usr/share/sounds/ubuntu/stereo/phone-outgoing-busy.ogg –  moose Mar 5 '11 at 20:43
    
printf '\7' works for me (paplay doesn't; (git shell)) but it's just a very small bip. Are there more codes that make other sounds? I tried \6, \8 but they are some characters. –  laggingreflex Sep 17 at 23:20
    
@laggingreflex No, \7 (BEL) is usually the only character that possibly triggers a sound (see the Wikipedia article Control character); note that it may not always make a sound, either. –  musiphil Nov 20 at 18:31
time python MyScript.py; play /path/so/sound.ogg

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

share|improve this answer
    
Yep, play, aplay, or paplay. (My Ubuntu system has aplay and paplay by default.) –  Mikel Mar 4 '11 at 23:20
1  
time python MyScript.py -n 40; paplay /usr/share/sounds/ubuntu/stereo/desktop-login.ogg worked, thanks. –  moose Mar 5 '11 at 20:46

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
share|improve this answer
2  
notify-more or notify-send could indeed be useful alternatives to playing a sound. –  Mikel Mar 4 '11 at 23:19
    
@Mikel I'm completely addicted to libnotify; everything on my system pops up notifications –  Michael Mrozek Mar 4 '11 at 23:31

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.