Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I start a script that is going to take a long time, I inevitably realize it after I've started the script, and wish I had a way of doing some kind of alert once it finishes.

So, for example, if I run:

and press can I run another command once it finishes?

share|improve this question
up vote 16 down vote accepted

You can separate multple commands by ;, so they are executed sequentally, for example: ; echo Finished

If you wish to execute next program only if the script finished with return-code 0 (which usually means it has executed correctly), then: && echo OK

If you want the opposite (i.e. continue only if current command has failed), than: || echo FAILED

You could run your script in a background (but beware, scripts output (stdout and stderr) would continue to go to your terminal unless you redirect it somewhere), and then wait for it: &
wait; echo Finished

If you have alredy run script, you could suspend it with Ctrl-Z, and then execute something like:

fg ; echo Finished

Where fg brings the suspended process to foreground (bg would make it run in backgroung, pretty much like started whith &)

share|improve this answer
This is all useful...if you remember to do it before you execute Otherwise...not so much. The goal is to set an alert on a process that's already started when you realize you need the alert. – mlissner Sep 11 '11 at 6:23
@mlissner Expanded my answer to cover this case – aland Sep 11 '11 at 6:29

Turns out this isn't that hard: You can simply type the next command into the window while the existing one runs, press enter, and when the first one finishes, the second command will automatically run.

I'm guessing there are more elegant ways, but this does seem to work.

Editing to add that if you want to run an alert after the command finishes, you can create these aliases in .bashrc, then run alert while it is running:

 alias alert_helper='history|tail -n1|sed -e "s/^\s*[0-9]\+\s*//" -e "s/;\s*alert$//"'
 alias alert='notify-send -i gnome-terminal "Finished Terminal Job" "[$?] $(alert_helper)"'
share|improve this answer
Unless the running script expects input at one point. – Daniel Beck Sep 11 '11 at 5:59
@Daniel - yeah...that's a problem. Shoot. – mlissner Sep 11 '11 at 6:07

You can also use bash's job control. If you started


then press ctrl+z to suspend it:

[1]+  Stopped       
$ bg

to restart the job in the background (just as if started it with &). Then you can wait for this background job with

$ wait N && echo "Successfully completed"

where N is the job ID (probably 1 if you didn't run any other background jobs) which is also displayed as [1] above.

share|improve this answer

A while ago I have written a script to wait for the end of another process. NOISE_CMD could be something like notify-send ..., given that DISPLAY is set correctly.


NOISE_CMD="/usr/bin/mplayer -really-quiet $HOME/sfx/alarm-clock.mp3"

function usage() {
    echo "Usage: $(basename "$0") [ PROCESS_NAME [ PGREP_ARGS... ] ]"
    echo "Helpful arguments to pgrep are: -f -n -o -x"

if [ "$#" -gt 0 ] ; then
    PIDS="$(pgrep "$PATTERN" "$@")"

    if [ -z "$PIDS" ] ; then
        echo "No process matching pattern \"$PATTERN\" found."

    echo "Waiting for:"
    pgrep -l "$PATTERN" "$@"

    for PID in $PIDS ; do
        while [ -d "/proc/$PID" ] ; do
            sleep 1


Without any argment, just make some noise immediately. This behavoir allows something like this for convenience (say you call script below

apt-get upgrade ; ~/bin/

Of course you can do many funny things with such a script, like letting an instance of wait for an instance of, that is waiting for some other command. Or executing a command just right after some task of a other logged in user has finished... >:D

A former version of the script above might be interesting, if you want to avoid a dependency on pgrep and accept to lookup process IDs yourself:


if [ "$#" -lt 2 -o ! -d "/proc/$1" ] ; then
  echo "Usage: $(basename "$0") PID COMMAND [ ARGUMENTS... ]"

while [ -d "/proc/$1" ] ; do
  sleep 1

exec "$@"

Slightly off-topic, but useful in similar situations: One might be interested in reptyr, a tool that "steals" a process from some (parent) shell and runs it in from current shell. I have tried similar implementations and reptyr is the prettiest and most reliable for my purposes.

share|improve this answer

If the process does not run on current tty, then another option would be:

watch -g ps -opid -p <pid>; mycommand
share|improve this answer

You must log in to answer this question.

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