1

please advice in following problem.

there is UNIX server. I have 3 separate commands which I can do in UNIX server:

 1. java com.documentum.server.impl.utils.TestConnection SERVER_NAME PORT_NUMBER do_method
 2. dmqdocbroker -t SERVER_NAME -p PORT_NUMBER -c getserver
 3. dmqdocbroker -t SERVER_NAME -p PORT_NUMBER -c getservermap <REPOSITORY_NAME>

Note: dmqdocbroker - shell script.

I want to include 3 commands into one java class and each command's output to be written to separate log file. But i don't know how to implement it.

Also, could you please advice how it can be checked in eclipse?

Please advice.

With regards!

Bharat Sinha
  • 13,973
  • 6
  • 39
  • 63
Bulat Makhmutov
  • 555
  • 2
  • 8
  • 14
  • 1
    Any reason you want to do it in a Java class rather than just writing a simple shell script? – Jon Skeet Sep 18 '12 at 06:05
  • @JonSkeet Thats been my initial thought also :-) Otherwise, Bulat, if you really need to do it from Java, please take a look at the exec() methods from the java.lang.Runtime class: http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html Please let us know if you have additional requirements which we do not understand so far ... – Andreas Fester Sep 18 '12 at 06:09
  • I think this might help you: http://stackoverflow.com/questions/3062305/executing-shell-commands-from-java – while Sep 18 '12 at 06:13

2 Answers2

1

Based on the other answers, here's what a shell implementation would look like:

#!/bin/sh
java com.documentum.server.impl.utils.TestConnection SERVER_NAME PORT_NUMBER do_method &> log_file_1
dmqdocbroker -t SERVER_NAME -p PORT_NUMBER -c getserver &> log_file_2
dmqdocbroker -t SERVER_NAME -p PORT_NUMBER -c getservermap <REPOSITORY_NAME> &> log_file_3

If you're curious, the command &> filename means that you're putting both standard output and error output to the same log file. If you want those to go to different log files, you can do any combination of > fileA for standard output and 2> fileB for standard error.

Finally, if you want to have those three commands happen simultaneously, you can put a bare & at the very end.

Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
  • thanks for information - i am curious if shell script can send email attaching the generated log files to list of recipients? – Bulat Makhmutov Sep 18 '12 at 06:48
  • That question is a pretty common one and there are [plenty of resources available](http://www.simplehelp.net/2008/12/01/how-to-send-email-from-the-linux-command-line/) through your favorite search engine. :) – Jeff Bowman Sep 18 '12 at 07:12
0

Take a look in api of Process and Runtime.exec. In Process, you can use getInputStream() and getErrorStream() to get the stdout and stderr of the process and do your redirection.

However, it does not make sense to me to write a java program for such purpose. Writing a shell script will be much simpler.

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • thanks for information Guys, i very appreciate it. The reason is simple, i don't have full knowleadge on shell scripting. but i have some experience on java programming. Do you have links for shell scripting docs for absolute beginner? – Bulat Makhmutov Sep 18 '12 at 06:14
  • @BulatMakhmutov: such a task is *much* easier in a shell script than in Java, so I suspect that even *without* prior knowledge, you'll be faster by doing it in a shell script. There's basically only one thing to know for you: a shell script is nothing else than a list of commands that you could equally enter at the shell, (usually) separated by newlines. So put all three of your commands in a file, prefix it with `#!/bin/sh` (in its own line) and you're pretty much done. – Joachim Sauer Sep 18 '12 at 06:20
  • Also have a brief search on google for shell script tutorial. There are plenty of them. You may look into topics including : redirecting stdout/stderr, running process in background, and waiting for process to complete. These topics should either be in shell scripting tutorials, or available thru a simple google search – Adrian Shum Sep 18 '12 at 06:33