Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从特定用户的java中执行shellscript_Java_Shell_Jakarta Ee - Fatal编程技术网

如何从特定用户的java中执行shellscript

如何从特定用户的java中执行shellscript,java,shell,jakarta-ee,Java,Shell,Jakarta Ee,我正在使用Runtime.getRuntime.exec调用shellscript 我的脚本被正确调用,但它作为根用户执行,我想用其他用户调用它 我该怎么做 我的命令是: 字符串包装器\u script=homedir+/blast\u distribute.sh+-seqs=+seqs+-i=+formobj.getUpFile.getFileName++formobj.getSelected\u程序 然后我使用:Runtime.getRuntime.execwrapper\u脚本 生成的进程

我正在使用Runtime.getRuntime.exec调用shellscript

我的脚本被正确调用,但它作为根用户执行,我想用其他用户调用它

我该怎么做

我的命令是:

字符串包装器\u script=homedir+/blast\u distribute.sh+-seqs=+seqs+-i=+formobj.getUpFile.getFileName++formobj.getSelected\u程序


然后我使用:Runtime.getRuntime.execwrapper\u脚本

生成的进程将作为运行JVM的同一用户执行。根据应用程序的不同,如果以root用户身份运行,可能不是一个好主意

但如果是,您可以使用Linux命令更改为其他用户,例如:

su <user> -c "<command>"
将以用户身份运行。

如果您的计算机上安装了sudo,请将适当的用户添加到/etc/sudoers,然后您可以使用Runtime.getRuntime.execsudo-u您的用户您的命令使用

runuser -l  userName -c 'command'
在exec函数中

这是我运行的命令的快照

或者,如果要从特定路径运行特定命令,请使用

runuser -l  userName -c 'path/to/Dir/command arg arg2 arg2'

我使用的Centos 6.2My命令类似于字符串包装器\u script=homedir+/blast\u distribute.sh+-seqs=+seqs+-I=+formobj.getUpFile.getFileName++formobj.getSelected\u程序;然后我使用:Runtime.getRuntime.execwrapper\u脚本;我应该如何修改我的代码?我已经尝试过字符串包装程序\u script=su username-c+homedir+/blast\u distribute.sh+-seqs=+seqs+-I=+formobj.getUpFile.getFileName++formobj.getSelected\u程序;但是它不能正常工作。您是否正确地转义了所有?是的……我认为当它看到su username时,它会切换到其他用户,并且命令的其余部分不会执行。其他用户是否在/etc/passwd中指定了有效的主目录和shell?