in reply to wait for process to finish
You can make the ssh process wait for the remote process (which in fact is the default if you don't start it in the background), and then you can just use wait or waitpid on the ssh process.