Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.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 runtime exec运行Linux上的连续命令_Java_Linux_Runtime - Fatal编程技术网

使用java runtime exec运行Linux上的连续命令

使用java runtime exec运行Linux上的连续命令,java,linux,runtime,Java,Linux,Runtime,我需要使用如下java代码在Linux上运行两个命令: Runtime rt = Runtime.getRuntime(); Process pr=rt.exec("su - test"); String line=null; BufferedReader input = new BufferedReader(new InputStreamReader(p

我需要使用如下java代码在Linux上运行两个命令:

 Runtime rt = Runtime.getRuntime();
          
           
            Process  pr=rt.exec("su - test");
            String line=null;
            BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
          
            while((line=input.readLine()) != null) {
                
                System.out.println(line);
            }
           pr = rt.exec("whoami");
             input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

             line=null;

            while((line=input.readLine()) != null) {
                 System.out.println(line);
            }               
            int exitVal = pr.waitFor();
            System.out.println("Exited with error code "+exitVal);              
        } catch(Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
        }
    Process  pr = rt.exec(new String[]{"/bin/sh", "-c", "cd /tmp ; ls"});
问题是第二个命令(“whoami”)的输出没有显示第一个命令(“su-test”)上使用的当前用户! 此代码有问题吗?

如中所述:

在单独的进程中执行指定的字符串命令

每次通过exec()执行命令时,它都将在单独的子进程中执行。这也意味着su的效果在返回后立即不存在,这就是为什么
whoami
命令将在另一个子进程中执行,同样使用最初启动程序的用户

su test -c whoami

将为您提供所需的结果。

在一般情况下,您需要在shell中运行这些命令。大概是这样的:

 Runtime rt = Runtime.getRuntime();
          
           
            Process  pr=rt.exec("su - test");
            String line=null;
            BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
          
            while((line=input.readLine()) != null) {
                
                System.out.println(line);
            }
           pr = rt.exec("whoami");
             input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

             line=null;

            while((line=input.readLine()) != null) {
                 System.out.println(line);
            }               
            int exitVal = pr.waitFor();
            System.out.println("Exited with error code "+exitVal);              
        } catch(Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
        }
    Process  pr = rt.exec(new String[]{"/bin/sh", "-c", "cd /tmp ; ls"});
但在本例中,这是行不通的,因为
su
本身正在创建一个交互式子shell。但您可以这样做:

    Process  pr = rt.exec(new String[]{"su", "-c", "whoami", "-", "test"});

另一种选择是使用
sudo
而不是
su
;e、 g

    Process  pr = rt.exec(new String[]{"sudo", "-u", "test", "whoami"});


注意:虽然上面没有一个实际需要这样做,但是最好将“命令行”组装成字符串数组,而不是让
exec
来进行“解析”。(问题是,
exec
s splitter不理解shell引用。)

如果希望以需要时命令将在子shell中执行的方式运行多个命令,请参见此处的响应


(使用ProcessBuilder模拟外壳)

缺少try语句。请始终发布一个可运行的示例。是否支持此上下文中的test作为用户帐户名?你能澄清一下吗!