I want to know if there is a way to listen to a process in Linux & unix - when it ends and what's the exit code of the process(script).

I don't want to write a script that will run X seconds and check by ps -ef | grep PID if the process is still alive. I want to know if there is a way that the process will notify me when it ended and what was its exit code.


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

    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:

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 &
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 &
wait $PID
echo $?

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

  • And how does wait works? How much CPU it will typically take from the system?
    – Nir
    Aug 15 '13 at 19:31
  • 1
    I do not know how exactly bash wait works. I do know that it does not take noticeable amount of system resources. You can review the source code here: git.savannah.gnu.org/cgit/bash.git/tree/builtins/wait.def
    – lesmana
    Aug 15 '13 at 20:03

if your process run as deamon, consider using upstart or monit, they are kind of standard ways:



They can monitor process, exit code, restart processes, write log, ...


With ptrace() you can attach a tracer to a process (or run a new one), set an hook with PTRACE_O_TRACEEXIT (linux >= 2.5.60) and sleep until the process exit, then with PTRACE_GETEVENTMSG get the exit status.

Here a sample implementation called stopper, I checked it with the attach option only and it required some few changes to get it working (eventually if needed I'll post the code somewhere).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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