0

I have Ubuntu in my machine where I have installed snmpd. To start stop I use below command:

  1. service snmpd start
  2. service snmpd stop

Now I have to execute this comments from java. I have tried with but it does not work.

Runtime.getRuntime.exe("service snmpd start")

So, could you tell me how to execute a service of Linux suse through java?

Any help would be great

Souvik
  • 1,219
  • 3
  • 16
  • 38
  • 2
    No, that's not what you tried. That doesn't even compile! – Joachim Sauer May 15 '13 at 09:05
  • 1
    *"Any help would be great"* Any question would be helpful. – Andrew Thompson May 15 '13 at 09:05
  • 2
    Please read [this article on `Runtime.exec()`](http://www.javaworld.com/jw-12-2000/jw-1229-traps.html). It's old, but it's still true (except that you could replace `Runtime.exec()` with `ProcessBuilder`, which has a nicer API, but still has the same pits to fall into). – Joachim Sauer May 15 '13 at 09:06
  • @AndrewThompson: rarely have I seen an article on an actual API (as opposed to "a concept") that has aged so well. – Joachim Sauer May 15 '13 at 09:09
  • @JoachimSauer Too true. There might be lots of good articles on JavaWorld, but that is the only one I have book-marked. :) – Andrew Thompson May 15 '13 at 09:11
  • @AndrewThompson: I don't even have it bookmarked. I google "Runtime exec won't" and click on the first hit ;-) – Joachim Sauer May 15 '13 at 10:20
  • @JoachimSauer OK I'll fess up. When I said 'book-marked' I actually meant it is in a little utility I wrote over the last few days that stores my most common comments. It is just a matter of selecting comments (shown in a `JTable`) that fit a question, then copying the resulting string into a comment. I quite like it already.. :) OK, I can see how Google would work as well. ;) – Andrew Thompson May 15 '13 at 10:26
  • Huh. An edit & the question **still** contains a code snippet that would not compile.. :-/ – Andrew Thompson May 15 '13 at 10:27

1 Answers1

1

Suddenly people started talking about a link instead of the question. The solution is easy. One need to wait for the process to complete. See below code.

Process process = Runtime.getRuntime().exec("service snmpd start");
process.waitFor();
SLePort
  • 15,211
  • 3
  • 34
  • 44
Souvik
  • 1,219
  • 3
  • 16
  • 38