This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes More Runtime questions Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "More Runtime questions" Watch "More Runtime questions" New topic
Author

More Runtime questions

Tom Hill
Ranch Hand

Joined: Aug 24, 2003
Posts: 115
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

Joined: Jul 08, 2003
Posts: 24051
    
  13

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.


[Jess in Action][AskingGoodQuestions]
Tom Hill
Ranch Hand

Joined: Aug 24, 2003
Posts: 115
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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: More Runtime questions
 
Similar Threads
Runtime getRuntime() exec(String command) - How does this work?
Problem with su - user_name inside script
How to get both ErrorStream and InputStream of a Process
running native applications through web server
running unix 'su 'cmd from java