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?
author and iconoclast
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.