See Salva's advice in channel SSH2. You probably are better off running a background process on the server itself. You exec with a nohup and detach the pid back into the background. You can log all output. Why do you need the logic of command processing done on the remote machine thru an unreliable network? Launch your control script right on the server, and come back later and get the log of it running.