Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Problem executing the script

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have to do a reboot of the linux box programatically and below is my code



When I call this method from my action class, it doesn't reboot the box and throws no exception. Any advise where I am wrong?
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It could be because sudo expects a password. If you are executing this as a regular user, try running it as root instead. I'm just guessing here though...
 
Mary Cole
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. What should be done so that it doesn't ask for the password. I need to execute it as that user instead of root
 
Jeff Rummings
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Mary Cole:
Thanks for your reply. What should be done so that it doesn't ask for the password. I need to execute it as that user instead of root



I doubt this will be possible. If you're trying to execute a command as a superuser, the OS will require a password. This has nothing to do with java. Unfortunately, I don't have a solution to this problem. Perhaps someone else can suggest a workaround.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can send the password using the Process object - it has a getOutputStream() method. Use that to send data to the process. It's also a good idea to set up threads which are reading from getInputStream() and getErrorStream(). For starters, just print these to System.out and System.err. Chances are good that, whatever problems you may have, the info in these streams will provide useful clues. Like an error message, or a prompt that says "password:".

I recommend reading When Runtime.exec() won't for more useful advice on dealing with exec(). Note that since JDK 5, ProcessBuilder is probably easier to use.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!