Is there a way to listen to process?

One -- admittedly heavy-handed -- approach is to use strace:

$ strace -e trace=none -e signal=none -p 12345

will watch the process with PID 12345, intercepting no system call (first -e) and no signals (second -e). Once the process exits in a regular way, the exit value will be printed.

If the process is terminated by a signal, strace exits silently (when run with the options given above). You can use e.g. -e signal=kill to change this behaviour. Note, however, that -e signal=all (or, equivalently, omitting the -e signal option) might produce a large amount of output if signals are received and handled by the program.


  1. Chaining the execution of "notify"

    $ process; notify $? &
    

    Notice that if the process will exit in unexpected way notify won't be executed

  2. Setting up traps

    Process is signalled by signals of a different meaning and can react appropriately

    #!/bin/bash
    
    process
    
    function finish {
        notify $?
    }
    trap finish EXIT
    

You are not clear what notification you have in mind. In essence it can be anything what rings a "bell" of course. One for many eg. notify-send from libnotify library.

$ process; notify-send "process finished with status $?" &

Bash does this for you. It will notify you when the process ends by giving you back control and it will store the exit status in the special variable $?. It look roughly like this:

someprocess
echo $?

See the bash manual about special parameters for more information.

But I asume that you want to do other work while waiting. In bash you can do that like this:

someprocess &
otherwork
wait %+
echo $?

someprocess & will start the process in the background. That means control will return immediately and you can do other work. A process started in the background is called a job in bash. wait will wait for the given job to finish and then return the exit status of that job. Jobs are referenced by %n. %+ refers to the last job started. See the bash manual about job control for more information.

If you really need the PID you can also do it like this:

someprocess &
PID=$!
otherwork
wait $PID
echo $?

$! is a special variable containing the PID of the last started background process.