Use stdout.isTTY instead of stdout_type == 'tty'.
Always try to remove pid on exit.