2012-07-10 42 views
1

我使用下面的腳本來啓動一個Java守護進程(命令經由根用戶啓動):重複過程經由外殼腳本推出

#!/bin/sh 
sudo -u postfix CONFIG_LOCATION=/mnt/custom java -Dcom.sun.management.jmxremote.port=10020 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.util.logging.config.file=${PresentWorkingDirectory}/logging.properties -cp "${ExecutableJar}:${PresentWorkingDirectory}${ClassPath}" com.x.y.filter <&- 1>/dev/null 2>&1 & 

這導致兩個運行的進程和我的發射在'ps -f -All'輸出中看到以下記錄:

4 S root  24250  1 0 82 0 - 26247 -  20:33 pts/1 00:00:00 sudo -u postfix CONFIG_LOCATION=/mnt/custom java -Dcom.sun.management.jmxremote.port=10020 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.util.logging.config.file=${PresentWorkingDirectory}/logging.properties -cp "${ExecutableJar}:${PresentWorkingDirectory}${ClassPath}" com.x.y.filter <&- 1>/dev/null 2>&1 & 
4 S postfix 24252 24250 47 82 0 - 364460 184466 20:33 pts/1 00:00:31 java -Dcom.sun.management.jmxremote.port=10020 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.util.logging.config.file=${PresentWorkingDirectory}/logging.properties -cp "${ExecutableJar}:${PresentWorkingDirectory}${ClassPath}" com.x.y.filter <&- 1>/dev/null 2>&1 & 

我無法理解爲什麼啓動兩個進程?

雖然我打算只運行一個進程,我的shell腳本也啓動只有一個進程。

有人能解釋一下上面的觀察嗎?

需要做些什麼來糾正?

回答

3

這是預期的行爲。你打電話給sudo,這是一個過程。這個過程會將其用戶更改爲postfix,然後致電java - 另一個過程。

如果sudo使用exec(以便有用於該命令只有一個進程),然後java將能夠運行的東西應該不是(因爲java二進制將取代內存中的sudo一個,等有所有的特權sudo),這可能是一個壞主意。

請注意,sudo二進制文件不會執行任何操作:在執行自己的清理之前,它只會等待java終止。

要理解的關鍵之一是sudo不是一個神奇的系統實用程序,它只是一個正常的應用程序,作爲setuid位。這意味着,允許sudo二進制文件更改它的運行時用戶uid。一旦你看到這個,你就開始明白sudo是如何工作的,以及你爲什麼得到兩個過程。