我在Linux Ubuntu服务器上编写了一个shell 脚本. 我想在Java上运行一个shell 脚本.

在Linux服务器中运行shell 脚本会改变我编写它的方式,但不是通过Java.

我将向您展示Java代码、shell 脚本文件和捕获屏幕.

Java

public class ShellScriptApplication {

    public static void main(String[] args) {

        String start = "start";
        String stop = "stop";
        String name = "";

        Scanner sc = new Scanner(System.in);
        System.out.print("start or stop? : ");
        String s = sc.nextLine();

        if(s.equals(start)) {
            System.out.print("시작하실 프로그램을 입력해주세요 [admin / ap / json / fixed] : ");
            name = sc.nextLine();
            openShell(start, name);
        } else if (s.equals(stop)) {
            System.out.print("종료하실 프로그램을 입력해주세요 [admin / ap / json / fixed] : ");
            name = sc.nextLine();
            openShell(stop, name);
        } else {
            System.out.println("wrong");
        }

    }

    public static String openShell(String status, String name)  {
        String result = "";

        String dir = "/data/GSLinkIN/app";

        String cdExec = "cd " + dir + " && ./"+ name + "-" + status + ".sh";

        String [] command = {"/bin/sh","-c", cdExec};
        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command(command);

        try {
            // Run script
            Process process = processBuilder.start();

            // Read output
            StringBuilder output = new StringBuilder();
            BufferedReader reader = new BufferedReader( new InputStreamReader(process.getInputStream()));

            String line;
            while ((line = reader.readLine()) != null) {
                output.append(line);
            }
            if(output.toString().equals("")) {
                result = "success";
            }else {
                result = output.toString();
            }
        } catch (Exception e) {
            result = "fail";
        }
        System.out.println(result);

        return result;
    }

}

stop.sh

#!/bin/sh

APP_NAME=linkin-admin-www-2.0.war
APP_PID=$(ps -ef | grep $APP_NAME | grep -v grep | awk '{print $2}')

echo "$APP_NAME (PID:$APP_PID)"
kill -9 $APP_PID
echo "$APP_NAME stopped"

When running a shell script using Java When running a shell script using Java

When running directly on a Linux server

When running directly on a Linux server

即使我在Java上运行一个Shell脚本,我也想打印这个ID,然后得到一个新行.

推荐答案

未识别新行

这句话很受欢迎,只是readLine

返回:

A String containing the contents of the line, not including any line-termination characters…

so you have 至 re-insert them if you want them in the result.
While you're at it, I suggest 至 shorten

            String line;
            while ((line = reader.readLine()) != null) {
                output.append(line);
            }
            if(output.至String().equals("")) {
                result = "success";
            }else {
                result = output.至String();
            }

            result = reader.lines().collect(Collec至rs.joining("\n"));
            if (result.equals("")) result = "success";

Java相关问答推荐

在没有maven或IDE的情况下从命令行运行PFA模块化应用程序时出现神秘错误

有没有方法可以修复错误错误:无法初始化主类code_editor?

是否有一种格式模式,可以在除0之外的数字前面有正负符号?

为什么接口中的主函数而不是类中的主函数在Java 17中编译和运行没有问题?

为什么如果数组列表中有重复项,我的代码SOMETIMES不返回true?

H2弹簧靴试验跌落台

参数值[...]与预期类型java.util.Date不匹配

路径映射未发生

上下文初始化期间遇到异常-使用Java配置配置HibernateTemplate Bean时

在模拟超类中设置非setter属性的值

如何以干净的方式访问深度嵌套的对象S属性?

如何让JFileChooser(DIRECTORIES_ONLY)从FolderName中的空白开始?

这是什么Java构造`(InputStream Is)->;()->;{}`

Java泛型类方法的静态返回类型是否被类型擦除?

将关闭拍卖的TimerService

向Java进程发送`kill-11`会引发NullPointerException吗?

除0错误/抱歉我的句子是PT

每次我需要时创建和关闭数据库连接会有什么效果吗?

如何在MPAndroidChart中的条形图上正确添加标签

Java 21保护模式的穷尽性