Added utilities.write_pid_to_file and implemented in CGS servers. Also set stdout to unbuffered by servers and tidied up exception error handling