2

I wrote a custom Java server. I am running it using Upstart (/etc/init/myservice.config) where I launch java -jar myservice.jar.

The java process uses System.out.println to write log messages of all kinds.

I want to be able to connect to it from a remote ssh session and view the output.

UPDATE: I want to do this without the hassle of creating, rotating, managing log files.

I've looked at a number of posts, including: How to view the output of a running process in another bash session?

I've tried cat and tail -f /proc/<procid>/fd/0 /proc/<procid>/fd/1 /proc/<procid>/fd/2 — all are blank output.

All this is as root

I know there are more things to try, but I'm suspicious since the fd's are blank (or at least for me).

Im running Ubunutu Server 14.04 LTS, Oracle Java 8

Update: Looking to try screen, but I see its not exactly meant for this... How do I start a screen session using an upstart job with a non privileged user?

Nick
  • 129
  • Why arent you using log4j or one of Java's libraries for logging? These types of libraries will log output to a txt or log file that you can tail. A system.out.println statement will NOT write to a log file.. – ryekayo Aug 18 '16 at 20:00
  • @ryekayo- i appreciate your indirect suggestion. log4j is an enormous PIA, especially with its newer version and the conflicting documentation. In a more direct answer, its because this is how it is written today, and I need a fast way to connect to it, rather than rewriting my app. Maybe one day I will. – Nick Aug 18 '16 at 21:36

2 Answers2

0

The easiest way I can think of is to redirect the output to a file like this:

java -jar myservice.jar >> /var/tmp/myservice

If there's a lot of output, the log can grow big, so you may want to trim it with a cron task every few hours or so, or to rotate logs.

Make sure it is System.out.println that prints the output, not System.err.println. If it's err, you need to redirect the error stream.

java -jar myservice.jar 2>> /var/tmp/myservice
  • yes, the log will get big... but perhaps this is good enough for now, before i can implement something more robust in the code itself. I think maybe i can use this in conjunction with rotatelogs... – Nick Aug 18 '16 at 22:55
  • You can put rotation in cron, in case it's not so readable in my answer. –  Aug 18 '16 at 23:02
0
java -jar myservice.jar > mylogfile.txt 2>&1

The 2>&1 means that you also log the errors from your program into mylogfile.txt

Timo
  • 11