java - 在服务器上运行 Shell 脚本

标签 java web-services servlets servlet-3.0

我有一个独立的应用程序,它在 Ubuntu 中运行 Shell 脚本(带参数)。

ProcessBuilder pb1 = new ProcessBuilder("sh","deltapackage_app.sh","part_value","pathtofile"); 
Process process1 = pb1.start();

我正在通过 GUI 获取参数。 现在我想在 Web 应用程序中实现同样的事情,我可以从网页中获取输入并将其发送到服务器,然后服务器将使用参数执行 shell 脚本。

任何人都可以建议我最好的方法吗?我应该用什么东西来做到这一点。

我知道我必须学习很多有关服务器的知识。或者我可以对基于浏览器的应用程序使用相同的代码。

最佳答案

Consider the following line of code:

Process p = Runtime.getRuntime().exec("/bin/sh -c /bin/ls > ls.out");

This is intended to execute a Bourne shell and have the shell execute the ls command, redirecting the output of ls to the file ls.out. The reason for using /bin/sh is to get around the problem of having stdout redirected by the Java internals. Unfortunately, if you try this nothing will happen. When this command string is passed to the exec() method it will be broken into an array of Strings with the elements being "/bin/sh", "-c", "/bin/ls", ">", and "ls.out". This will fail, as sh expects only a single argument to the "-c" switch. To make this work try:

    String[] cmd = {"/bin/sh", "-c", "/bin/ls > out.dat"};
    Process p = Runtime.getRuntime().exec(cmd);

Since the command line is already a series of Strings, the strings will simply be loaded into the command array by the exec() method and passed to the new process as is. Thus the shell will see a "-c" and the command "/bin/ls > ls.out" and execute correctly.

http://www.ensta-paristech.fr/~diam/java/online/io/javazine.html

关于java - 在服务器上运行 Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11240504/

相关文章:

java - SWT 文本小部件不显示

java - 在 Firebase (Android) 中同时更新多个节点中的多个字段

java - 当客户端停止工作时,JMS 有 javax.jms.InvalidDestinationException

c# - 如何设置 JSON 对象从结果开始?.Net MySQL JSON Restful Webservice?

java - Guice 和 URLRewrite 过滤器集成

java - Lombok @Builder 的必需参数

web-services - 适用于多个 Web 服务的 Azure Key Vault 最佳实践?

android - 使用 JSON 通过 Android 访问 .NET Web 服务

java - 长时间关机时如何处理servlet请求

java - Spring MVC 从 RequestMapping 引用 params 变量