可以在我的VPS中使用ProcessBuilder执行脚本(.sh)和打印错误
我正在尝试在Java中执行函数,以执行Linux VPS中的.SH脚本(关闭)。
ProcessBuilder pb = new ProcessBuilder("/bin/bash", "mkdir", "test"); //here I want to change "mkdir" and "test" by shutdown.sh
pb.directory(new File("/opt/tomcat/transfert/"));
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
ArrayList<String> lines = new ArrayList<String>();
while ((line = reader.readLine()) != null) {
lines.add(line);
lines.add(System.getProperty("line.separator"));
}
request.setAttribute("lines", lines);
我在Windows 10上工作,然后通过VPS上的Tomcat服务器通过,因此在VPS上执行时看不到任何控制台(这是因为我通过请求中的行中的行以显示在我的WebContent中)。 如果您知道如何在VPS输出上打印错误,它将很酷。
但是在执行时,什么也没做,我的“ Transfert”文件夹没有任何更改,也没有返回我的网页。
在我的Debian VPS中,我更新了不同的文件和文件夹的权限,以执行和可读。
我在另一个主题中看到了问题,问题可能是ProcessBuilder使用的“用户”没有正确的许可,而是如何更改?
因此,我不明白为什么会发生这种情况,因为我更改了代码中的所有内容,并搜索了许多正确运行的方式,并且不想工作...
在之后写:如果我尝试命令直接在我的VPS控制台中,有效!
update :
我也尝试这样做:
String[] cmd = {
"/bin/sh",
"-c",
"mkdir /home/debian/test"
};
Process proc = Runtime.getRuntime().exec(cmd);
但这不起作用...
thx,etienne
I'm trying to do a function in Java for execute an .sh script (shutdown) in a linux vps.
ProcessBuilder pb = new ProcessBuilder("/bin/bash", "mkdir", "test"); //here I want to change "mkdir" and "test" by shutdown.sh
pb.directory(new File("/opt/tomcat/transfert/"));
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
ArrayList<String> lines = new ArrayList<String>();
while ((line = reader.readLine()) != null) {
lines.add(line);
lines.add(System.getProperty("line.separator"));
}
request.setAttribute("lines", lines);
I work on a windows 10 and I pass by a tomcat server on my vps so I can't see any console during execution on the vps (it's for that I pass lines in my request to display this in my webcontent). If you know how can I print error on the vps output it will be cool.
But at the execution, nothing is done, there is no change in my "transfert" folder and nothing is return to my web page.
In my debian vps, I updated the differents files and folders permission to be executable and readable.
I saw in an other topic the problem can be the "user" used by the ProcessBuilder haven't the right permission but how to change that ?
So I don't understand why this happend because I change everything in my code and search about many way to run correctly and that don't want to work...
Written after : If I try the command directly in my vps console, that works !
Update :
I also try to do it like this :
String[] cmd = {
"/bin/sh",
"-c",
"mkdir /home/debian/test"
};
Process proc = Runtime.getRuntime().exec(cmd);
But that don't work more...
Thx, Etienne
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
运行命令
/bin/bash mkdir Test
如果直接调用,可能在系统上不起作用。您需要“ -c”才能使bash启动另一个命令:使用绝对路径并扫描错误消息是更安全的,这是通过合并错误 - &gt;这是最容易的。如果
shutdown.sh
以“#!/bin/sh”开头,则不需要直接使用“/bin/bash”。Running the command
/bin/bash mkdir test
probably does not work on your system if called directly. You need "-c" to make bash launch another command:It is safer to use absolute path and scan the error message too, this is easiest by merging error -> stdout and there should be no need to use "/bin/bash" directly if
shutdown.sh
starts with "#!/bin/sh" :