Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set specific user to run processBuilder?

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

like image 879
rsijaya Avatar asked Mar 14 '26 07:03

rsijaya


1 Answers

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.

like image 185
rzwitserloot Avatar answered Mar 16 '26 20:03

rzwitserloot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!