3

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.

4 Answers 4

5

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.

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
4
  • 1
    Screen - 3rd party??
    – Linker3000
    Feb 10, 2011 at 23:35
  • @Linker3000 I meant additional xD but couldn't find the correct word when writing my edit...
    – Pylsa
    Feb 10, 2011 at 23:37
  • 1
    you can then watch the log file in near realtime with 'tail scriptlog.log'
    – Journeyman Geek
    Feb 11, 2011 at 0:00
  • or (possibly) even better; tail -f scriptlog.log
    – Eroen
    Apr 20, 2012 at 22:43
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.

1

I doubt any of the existing answer helps in your case, where you already run your program without screen/tmux/nohup.

Try strace when the program is still running:

strace -p $PID -e trace=write

If you can identify the fd number of the stdout (might take some guess), you can add a |grep -F "write(1" to the command (here we assume stdout=fd 1).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .