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!

3
  • 1
    Any reason you want to do it in a Java class rather than just writing a simple shell script? Commented Sep 18, 2012 at 6: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: 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 ... Commented Sep 18, 2012 at 6:09
  • I think this might help you: stackoverflow.com/questions/3062305/… Commented Sep 18, 2012 at 6:13

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for information - i am curious if shell script can send email attaching the generated log files to list of recipients?
That question is a pretty common one and there are plenty of resources available through your favorite search engine. :)
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.

3 Comments

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?
@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.
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.