繁体   English   中英

Runtime.exec()无法运行“su - postgres -c'pg_dump ...'”

[英]Runtime.exec() can't run “su - postgres -c 'pg_dump …'”

这是我想要运行的命令:

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

我现在应该怎么做?

exec(new String[]{"sh", "-c", "su - postgres ..."});

除了上面给出的答案(并且理解你的问题很好),请记住,只需将命令放在shell脚本文件中,就可以让生活更轻松。 例如, dumppg.sh文件只包含两行:

#!/bin/sh
su - postgres -c "pg_dump ....."

只是使它可执行,并(测试后)从java调用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM