2011-06-29 72 views
0

如何修改下面的ruby腳本以使其工作? (我是在Linux上。)Ruby:如何從ruby腳本內部啓動java服務器

# 1. Launch a java server: 
system "java -jar myJavaServer.jar -someArgument -otherArgument" 

# 2. Execute some ruby code (with the server running): 
[...] 

# 3. Stop the java server: 
system "killall java" 

我想是這樣下面的代碼獲取與服務器中運行執行的Java服務器必須啓動到一個單獨的紅寶石線程...?

非常感謝您的幫助!

湯姆

回答

0

使用fork()運行在一個過程中的Java服務器和其他Ruby代碼。

pid = fork() 
if pid then 
    # This is the parent process. Execute your ruby code here 
    # then kill the process running the java server 
    system "kill #{pid}" 
else 
    # this is the child process, run the java server here 
    system "java -jar myJavaServer.jar -someArgument -otherArgument" 
end 
+0

謝謝,但不幸的是,這似乎阻止服務器啓動。 (我收到與&符號的錯誤消息...) – TomDogg

+0

嗯。好的,這個怎麼樣。編寫一個在後臺啓動java服務器的shell腳本(用&),執行你的ruby腳本,然後執行「killall java」。 –

+0

我沒有提到它,但它實際上是一個Rails應用程序,應該執行ruby腳本......所以我寧願從這個ruby腳本里面做所有的事情 - 如果有人有這個解決方案...... – TomDogg

相關問題