• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

More Runtime questions

 
Tom Hill
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I'm trying to use the Runtime class to execute some functions in a unix shell.

I want to be able to run two processes keeping the environment the same environment eg:
" su - myusername "
"somecommand that user can do"

The problem using : Process p = Runtime.getRuntime.exec("su - myusername"); is that the environment is lost when I run:
Process q = getRuntime.exec("somecommand that user can do");
Or at least I think it is. Any ideas?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you execute "su", the process that is started is an interactive shell. So in theory, what you could do would be to start the "su" process, then send subsequent commands to that shell's standard input -- i.e., call Process.getOutputStream() and print the commands to the OutputStream that you get back.

Alternatively, if you only need to run one command, look at the -c switch to su, which lets you run a command; or use "sudo", which is similar.

Now in practice, this is only going to work for userids that you can "su to" without a password, because if su reads a password, it does it by opening the raw terminal device, and you won't be able to intercept that from Java.
 
Tom Hill
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are the most obvious things also the hardest to get right. Thanks for all the help - I had misunderstood how the shell was accessed by java - all is good!

Cheers!

Tom
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic