See process output after being disconnect from a SSH session

1

I'm connecting to my web host from Windows 7 using putty ssh. When I start a script in the remote host, sometimes I get disconnected. I have to connect again, and from the command "ps aux" I can see that my script is still running, but I don't have the outputs on terminal anymore.

How can I see the script outputs after re-logging?

Thank you.

EduardoBR

Posted 2011-02-10T23:17:28.933

Reputation: 11

Answers

4

You should consider running GNU Screen after logging in via ssh. There's a ton you can do from there, which you can read about on the net. The basic functionality will give you a new shell which will (magically) continue to run even after you've closed your putty window.

When you reconnect to the server, just start screen with the "-R" option and you'll reconnect to your existing session. Magic.

It's likely already installed on your system; try typing "screen" at the shell prompt.

Once you start to use screen you'll wonder how you lived without it. For an enhanced screen experience, try "byobu". It's preinstalled on newer versions of Ubuntu.

Ryan Dlugosz

Posted 2011-02-10T23:17:28.933

Reputation: 151

1

You cannot without installing/configuring additional software, but alternatively you could redirect the output of the script to a log file and watch that log file after the script has completed or while it is running by executing this command to execute your script:

myscript.sh > ~/scriptlog.log

The outputs will then be written to scriptlog.log in your home directory.

As The Journeyman Geek stated in his comment:

You can then watch the log file in near realtime with

tail scriptlog.log

BloodPhilia

Posted 2011-02-10T23:17:28.933

Reputation: 27 374

1Screen - 3rd party?? – Linker3000 – 2011-02-10T23:35:15.957

@Linker3000 I meant additional xD but couldn't find the correct word when writing my edit... – BloodPhilia – 2011-02-10T23:37:14.157

1you can then watch the log file in near realtime with 'tail scriptlog.log' – Journeyman Geek – 2011-02-11T00:00:46.647

or (possibly) even better; tail -f scriptlog.log – Eroen – 2012-04-20T22:43:50.507

1

If you don't need the features or want the overhead of any of these typical tools for managing detachable processes:

  • GNU screen
  • byobu
  • dtach
  • tmux
  • vnc
  • xpra

you can keep track of the output from a process by prefixing your command with nohup when you start it up. By default, it will save both standard output and standard error to nohup.out, but see the man page for additional details.

nohup won't let you interact with the process once it's been detached, but it will allow the script to complete and allow you to view the output after the fact. For more complex cases, such as long-running interactive processes or X clients, you may want to look at one of the other tools listed and see if they fit your needs better.

CodeGnome

Posted 2011-02-10T23:17:28.933

Reputation: 1 841