Killing a process synchronously

Here is what I try to achieve : I want to run a process and be able to kill it if needed. When the process is either killed or terminates normally, there is a piece of code that I always want to run.

The issue I see with @lud’s solution is the following : To monitor a process, you need a pid. So when you call the monitor function the process is normally already running. It may even be over by the time you call the monitor function, in which case you will never receive the DOWN message you expect (this DOWN message would trigger the piece of code you always want to execute when a process finishes).

Am I making sense ?