2

I run a java application as a server on a EC2. But as I loose my ssh connection all console messages are also lost. When I redirect all messages to > my.log and then use tail -f my.log I dont get the e.printstacktrace messages. I wonder what I can do.

Is there something simple that I can do, to relogin to ssh, find out which console the java application is throwing out all the messages.

So instead of doing a java myapp.jar &, I now do a java myapp.jar, so that I dont loose any console messages.

The other question is, Is there a better command than > my.log that does not miss any message from my application, so that I can log, and do a tail -f my.log ?

2 Answers 2

3

I would use a terminal multiplexer - screen being the best known, and tmux being a more recent implementation of the idea. I use tmux, and would recommend you do to.

Basically tmux will run a terminal (or set of terminals) on a computer. If you run it on a remote server, you can disconnect from it without the terminal dying. Then when you login in again later you can reconnect, and see all the output you missed.

To start it the first time, just type

tmux

Then, when you want to disconnect, you do Ctrl-B d (ie press Ctrl-B, then release both keys, and then press d.

When you login again, you can run

tmux attach

and you will reconnect to tmux and see all the output that happened. Note that if you accidentally lose the ssh connection (say your network goes down), tmux will still be running, though it may think it is still attached to a connection. You can tell tmux to detach from the last connection and attach to your new connection by running

tmux attach -d

In fact, you can use the -d option all the time. On servers, I have this in my .bashrc

alias tt='tmux attach -d'

So when I login I can just type tt and reattach. You can go one step further if you want and integrate the command into an alias for ssh. I run a mail client inside tmux on a server, and I have a local alias:

alias maileo='ssh -t mail.example.org tmux attach -d'

This does ssh to the server and runs the command at the end - tmux attach -d The -t option ensures that a terminal is started - if a command is supplied then it is not run in a terminal by default. So now I can run maileo on a local command line and connect to the server, and the tmux session. When I disconnect from tmux, the ssh connection is also killed.

This shows how to use tmux for your specific use case, but tmux can do much more than this. This tmux tutorial will teach you a bit more, and there is plenty more out there.

3
  • Billiant, I'll try it and report back.
    – Siddharth
    Jul 15, 2012 at 11:33
  • works like a charm. I am trying to figure out why my mouse scroll wheel wont work on tmux.
    – Siddharth
    Jul 16, 2012 at 4:01
  • @Siddharth Yes, the mouse won't work with tmux. Instead you have to do Ctrl-A [ to enter scrollback mode. You can then PgUp and PgDown. When you're done, press q to go back to normal mode. (And feel free to accept my answer ;) Jul 17, 2012 at 9:11
2

You have a few problems if you want to leave the process running when you disconnect and see all the output later.

Capturing Error Messages

One problem with your approach is that you are only redirecting stdout, but not stderr, so error messages will not go into the file. The standard idiom to redirect all output is:

java myapp.jar 2>&1 > myapp.log

This explains what 2>&1 means and this gives chapter and verse on bash i/o redirection.

If you want to see the difference, you could do

java myapp.jar > myapp.log 2> myapp.err

Leave process running when you disconnect

Even running in the background (by appending & to the command), a process is still attached to a terminal. When the terminal dies, all subprocesses will be killed, including those in the background.

To keep them alive you can

  • keep the terminal alive despite losing your connection by using a terminal multiplexer (see my other answer or,
  • use nohup which will keep the command running (by ignoring SIGHUP).

To use nohup, redirecting all output to a log and putting it into the background, you can just do:

nohup java myapp.jar 2>&1 > myapp.log &

More about nohup.

You must log in to answer this question.

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