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

share|improve this question

5 Answers 5

up vote 3 down vote accepted

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.

share|improve this answer
    
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
  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 $?" &
share|improve this answer

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

http://www.alexreisner.com/code/upstart

http://mmonit.com/monit/

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

share|improve this answer

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.

share|improve this answer

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

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.