How to set specific user to run processBuilder?
ProcessBuilder processBuilder = new ProcessBuilder();
String[] cmdStr = {"su", "-s", "/bin/bash", "myUser", "-c", "/home/myUser/sch.sh", "argumen"};
processBuilder.command(cmdStr);
Process process = processBuilder.start();
when i try this is not working
Always specify paths, so use /bin/su. However, more generally, su cannot be used except if you have a full interactive terminal, because it will ask for a password and you can't provide it, because that is rife with security problems and therefore su just generally won't allow it.
The advised strategy to do such a thing is to use sudo instead, and mark the specific script you want to run as owned by root, and marked off especially in your sudoers file as 'the user that runs the java process can sudo this specific shell script without a password, but nothing else'. Or, in this case, not root, but myUser.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With