简体   繁体   中英

How to invoke shell script as engineering user from java code

  final String[] cmdArray = {
 "/bin/bash", "/var/tmp/invoke_payment_files_generator.sh",
 String.valueOf(paymentBatchId)
 };


 final ProcessBuilder processBuilder = new ProcessBuilder(cmdArray); 

processBuilder.redirectErrorStream(true);

above code works for me but is their a way to invoke this shell script as a engineering user instead of unix user from java code.Can anyone suggest the way and can anyone suggest how to know by which user shell script is being invoked from java code

you could use sudo to run the script as another user. If the path is fixed, just add this to your sudoers file:

<jvmuser> ALL=(<execuser>) NOPASSWD: "/bin/bash /var/tmp/invoke_payment_files_generator.sh"

Replace with the shelluser and with the user running the java command.

Then modify your java shell command to run this:

sudo -u <execuser> /bin/bash /var/tmp/invoke_payment_files_generator.sh

It is untested and may need some tweaks, but the general idea should work just fine.


Another approach would just call a API (unix domain socket, web-servlet, file, sql queue table, rpc, etc.) which in return runs the payment file generator script for you, if you do not need the output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM