这是我要运行的命令:
su - postgres -c "pg_dump ....."
备份 postgres 数据库。
如果我现在在 linux shell 中,作为 root,它工作得很好。
但是现在,我想从 java 应用程序运行它,如:
String cmd = "su - postgres -c \"pg_dump --port 5432 .....\""
Process p = Runtime.getRuntime().exec(cmd);
// read the error stream and input stream
p.waitFor();
它抛出一个错误:
su: unknown option "--port"
please try "su --help" to get more information
现在我将代码更改为:
Process p = Runtime.getRuntime().exec(new String[0]{cmd});
// read the error stream and input stream
p.waitFor();
Cannot run program "su - postgres -c \"pg_dump .....\"": java.io.IOException: error=2, no that file or directory
我现在该怎么办?
最佳答案
除了上面给出的答案(理解您的问题是很好的)之外,请记住,只要将您的命令放入 shell 脚本文件中,您总是可以让您的生活更轻松。例如,一个只包含两行的 dumppg.sh
文件:
#!/bin/sh
su - postgres -c "pg_dump ....."
只需使其可执行,然后(在测试之后)从 java 中调用它。
关于java - Runtime.exec() 无法运行 "su - postgres -c ' pg_dump .. .'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4564635/