1
我想實現一個使用Runtime.getRuntime().exec()方法從Java代碼重新啓動PostrgeSQL服務器的功能。如果PostgreSQL服務器未啓用SSL,但該方法工作正常,但如果PostreSQL服務器啓用SSL並且私鑰使用密碼加密,則需要傳遞通過相。我試着用下面的代碼來實現它,將密碼傳遞給Runtime.getRuntime()。exec
String postgreSQLRestartCMD =
"/u/postgreSQL/pg_ctl -D /u/postgreSQL/data restart -w";
//-w option waits until the passphase is sent
final Process restartPosgreSQLServer = Runtime.getRuntime().exec(postgreSQLRestartCMD);
PrintStream ps = new PrintStream(restartPosgreSQLServer.getOutputStream());
ps.println("keyPassword"); // sending passphasse here
new Thread(new Runnable() {
@Override
public void run() {
try {
IOUtils.copy(restartPosgreSQLServer.getInputStream(), logWriter);
}
catch (final IOException e) {
logWriter.println("Error occurred while reading InputStream.");
e.printStackTrace(logWriter);
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
IOUtils.copy(restartPosgreSQLServer.getErrorStream(), logWriter);
}
catch (final IOException e) {
logWriter.println("Error occurred while reading ErrorStream.");
e.printStackTrace(logWriter);
}
}
}).start();
int returnStatus = restartPosgreSQLServer.waitFor();
if (returnStatus == 1) {
logWriter.println("Error has occured while running PostgreSQL server.");
} else {
logWriter.println("PostgreSQL has started successfully.");
}
但是服務器沒有開始。我正在嘗試在Linux中。在控制檯中,它被下線擊中,我猜passohase沒有正確發送。
Enter PEM pass phrase:
有人能指出我哪裏出錯了,爲什麼不發送通過?任何建議或幫助將非常感激。請幫助我。提前致謝。編輯:進一步挖掘我剛剛發現Runtime.getRuntime().exec(cmd)正在創建新進程,我的ps.println(「密碼」)被傳遞給父進程。但我不確定,我的觀察是否是儀式。請幫助我。