【问题标题】:Java how to stop shell script launched previously in the programJava如何停止以前在程序中启动的shell脚本
【发布时间】:2014-04-16 09:50:58
【问题描述】:

我创建了一个服务器程序,当它接收到一些命令时,它会执行一些操作。 我希望当它收到命令“android”(当客户端从 android 设备连接时发生)时,我的服务器会启动一个 shell 脚本,并且当客户端断开连接时,我希望它停止执行此脚本。

此外,当网络浏览器客户端连接并发送命令“浏览器”时,服务器将启动另一个脚本。

这两个脚本不能同时使用,因为它们使用相同的资源。这就是为什么我需要在客户端断开连接时停止它们(我知道我不能同时连接两种类型的客户端)。

目前,我已经能够启动脚本,但不能阻止它们。这是我在服务器中运行方法的代码:

@Override
public void run() {
    try {
        BufferedReader ins = new BufferedReader(new InputStreamReader(soc.getInputStream()));

        Runtime r = Runtime.getRuntime();
        Process p = null;

        int i = 0;
        while (i < 1) {
            String request = ins.readLine();
            if (request == null || request.equals("close")) {
                p.destroy();
                System.out.println("The child was destroyed? "+p.exitValue());
                i++;
            }
            else {
                if (request.equals("android")) {
                    p = r.exec("sh /home/pi/test-raspicamsrc.sh &");
                }
                else if (request.equals("browser")) {
                    p = r.exec("sh /home/pi/test-http-launch.sh &");
                }
                else if (request.equals("left")) {
                    serialPort.writeBytes("4".getBytes());//Write data to port
                }
                else if (request.equals("right")) {
                    serialPort.writeBytes("6".getBytes());
                }
                else if (request.equals("stop")) {
                    serialPort.writeBytes("5".getBytes());
                }
                else if (request.equals("forward")) {
                    serialPort.writeBytes("8".getBytes());
                }
                else if (request.equals("backward")) {
                    serialPort.writeBytes("2".getBytes());
                }
                else {
                    System.out.println("Unknown command");
                }
            }
        }
        ins.close();
        soc.close();
        // We always stop the car in the end.
        serialPort.writeBytes("5".getBytes());
    }

我第一次断开android设备时,p.exitValue()的输出是143,脚本一直在执行。之后,如果我再次与 android 客户端连接,它似乎不会再次启动脚本(这是一件好事,我不知道为什么或者它确实启动了它,但因为资源已经在使用中,它关闭得很快),当我断开这个新客户端p.exitValue() 时,返回0。这是正常的事情,因为该过程不存在。但在此期间,启动的第一个脚本的进程一直在运行。

我想知道为什么我的命令 p.destroy() 不会杀死我的脚本进程。我认为这可能是因为它停止了 sh 进程而不是我的脚本的进程(两个不同的进程)有点像这里java Process stop entire process tree

希望有办法解决这个问题,谢谢! :)

【问题讨论】:

    标签: java linux shell process


    【解决方案1】:

    您可以通过以下命令检查您的脚本是否在服务器中运行。

    ps -efw | grep '进程名称'

    现在要从 java 程序中停止您的脚本,您可以执行以下命令。

    pkill -9 -f 'test-raspicamsrc.sh'

    pkill 命令用于通过提供进程模式来杀死进程。如果你想在没有 pid 的情况下杀死你的 firefox 应用程序,你可以像下面这样执行。

    pkill -9 -f '火狐'

    【讨论】:

    • Mmmmh... 当我在 shell 中执行此命令 pkill -9 -f 'gst-launch-1.0' 时,它确实会杀死正确的进程,但是当我在程序中使用 killProcess = r.exec("pkill -9 -f 'gst-launch-1.0'"); 时,什么也没有发生!
    【解决方案2】:

    好的,我已经找到了如何让它工作。实际上我不得不使用pkill,但是使用我的脚本中执行的命令的进程的名称。此外,我不得不改变我在 Java 中执行命令的方式。因为在 shell 中启动 pkill -9 -f 'gst-launch-1.0' 会正确停止脚本,但在 java 代码中 killProcess = r.exec("pkill -9 -f 'gst-launch-1.0'"); 是无效的。所以我通过这种方式改变了我执行shell命令的方式:killProcess = r.exec(new String[]{"bash","-c","pkill -9 -f 'gst-launch-1.0'"});(根据这个主题:Want to invoke a linux shell command from Java

    这是我的工作代码:

    @Override
        public void run() {
            try {
                BufferedReader ins = new BufferedReader(new InputStreamReader(soc.getInputStream()));
    
                Runtime r = Runtime.getRuntime();
                Process videoProcess = null;
                Process killProcess = null;
                boolean android = false;
    
                int i = 0;
                while (i < 1) {
                    String request = ins.readLine();
    
                    if (request == null || request.equals("close")) {
                        if (android) {
                            killProcess = r.exec(new String[]{"bash","-c","pkill -9 -f 'gst-launch-1.0'"});
                        }
                        else {
                            killProcess = r.exec(new String[]{"bash","-c","pkill -9 -f 'http-launch'"});
                        }
                        i++;
                    }
                    else {
                        if (request.equals("android")) {
                            videoProcess = r.exec(new String[]{"bash","-c","sh /home/pi/test-raspicamsrc.sh"});
                            android = true;
                        }
                        else if (request.equals("browser")) {
                            videoProcess = r.exec(new String[]{"bash","-c","sh /home/pi/test-http-launch.sh"});
                            android = false;
                        }
                        else if (request.equals("left")) {
                            serialPort.writeBytes("4".getBytes());//Write data to port
                        }
                        else if (request.equals("right")) {
                            serialPort.writeBytes("6".getBytes());
                        }
                        else if (request.equals("stop")) {
                            serialPort.writeBytes("5".getBytes());
                        }
                        else if (request.equals("forward")) {
                            serialPort.writeBytes("8".getBytes());
                        }
                        else if (request.equals("backward")) {
                            serialPort.writeBytes("2".getBytes());
                        }
                        else {
                            System.out.println("Unknown command");
                        }
                    }
                }
                ins.close();
                soc.close();
                // We always stop the car in the end.
                serialPort.writeBytes("5".getBytes());
    
            }
            catch (IOException e) {
                System.out.println("Error input/output while initializing the server (Port 6020 may already be in use)");
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多