
我正在使用 OpenSUSE,並且創建了一個在 x11vnc 伺服器啟動時啟動的腳本。但是當使用者登出時x11vnc被殺死。我想讓它自動重新啟動。這是我寫的腳本。它在啟動時完美運行。
#!/bin/sh
#
# /etc/init.d/vnc
#
### BEGIN INIT INFO
# Provides: x11vnc server
# Required-Start: xdm
# Should-Start:
# Required-Stop:
# Should-Stop:
# Default-Start: 5
# Default-Stop: 0 1 2 6
# Short-Description:
# Description: Start or stop vnc server
### END INIT INFO
#INIT SCRIPT VARIABLES
SERVICE=$(basename $0)
#Gets the name of the script
BIN="/usr/bin/x11vnc"
#Binary path
ALLOWED_GROUP=$(getent group g_vnc-usr | awk -F ":" '{ print $4 }')
#Only inf-usr group is allowed to take control of any machine.
AUTH=`ps wwaux | grep '/X.*-auth' | sed -e 's/^.*-auth *//' -e 's/ .*$//' | head -n 1`
OPT="-display :0 -auth ${AUTH} -nopw -unixpw ${ALLOWED_GROUP} -shared -oa /var/log/vnc.log -xkb -bg -verbose -forever"
#Various options of the x11vnc providing auth, user auth, logging and "keep alive" connection.
CMD="${BIN} ${OPT}"
#Both bin and options are stored
. /lib/lsb/init-functions
rc_reset
# Reset status of this service
case "$1" in
start)
echo -n "Starting ${SERVICE}..."
## Start daemon with startproc(8).
/sbin/startproc ${CMD}
##>> /dev/null 2>&1
sleep 2s
# Remember status and be verbose.
rc_status -v
;;
stop)
echo -n "Shutting down ${SERVICE}..."
## Stop daemon with killproc(8)
/sbin/killproc ${BIN}
# Remember status and be verbose
rc_status -v
;;
restart)
## Stop the service and regardless of whether it was
## running or not, start it again.
$0 stop
$0 start
# Remember status and be quiet
rc_status
;;
status)
echo -n "Checking for service ${SERVICE}..."
## Check status with checkproc(8), if process is running
## checkproc will return with exit status 0.
/sbin/checkproc ${BIN}
# Remember status and be verbose
rc_status -v
;;
*)
echo -n
echo -n "Usage: ${SERVICE} {start|stop|restart|status}"
exit 1
;;
esac
rc_exit
該腳本允許該群組中的任何使用者接管計算機,即使當前沒有人登入也是如此。
我想使用xinitrc
並添加exec /etc/init.d/vnc restart
謝謝。
答案1
如果使用 systemd,比此處其他建議更好的解決方案是添加 systemd 單元覆蓋文件,正如係統作者設計和建議的那樣。
這正是所需的,並且定制最少,並且對未來維護和升級的影響最小。
如果您的基本 systemd 服務(不含 :INTEGER 後綴)的命名vncserver@
與我的一樣,您將建立目錄並將該檔案放入其中,名稱為。/etc/systemd/system/[email protected]/
override.conf
[Service]
Restart=on-success
RestartSec=10
然後運行systemctl daemon-reload
。根據系統消除先前會話進程的速度來調整 RestartSec。
您可以選擇運行,而不是手動建立目錄和文件
systemctl edit vncserver@
並在樣板文字指示的位置輸入上面的文字。使用此方法,您只需要知道基本服務名稱,而不是目錄,編輯命令將為您處理守護程序重新載入。
答案2
(將評論轉換為答案)
此-loop
參數可用於循環重新啟動 x11vnc。從手冊頁:
建立一個外部循環,在 x11vnc 進程終止時重新啟動該進程。在此模式下,-bg 和 -inetd 被忽略(但請參見下面的 -loopbg)。
即使 X 伺服器終止並重新啟動,也有助於繼續(當然,此時該進程需要重新連接到新 X 伺服器的權限)。
答案3
在 RHEL 機器上,使用 systemctl,我進行了以下更改。我添加了一個“/bin/at now”命令在一分鐘後啟動服務。就我而言,我將停止命令更改為:
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill -9 %i > /dev/null 2>&1; echo "systemctl start vncserver@:1.service" | /bin/at now + 1 minute || :'
我編輯:/etc/systemd/system/vncserver@:1.service
進行更改後,請執行以下命令來更新系統:systemctl 守護程式重新載入
答案4
您需要在服務文件的部分中添加另外兩個命令:Restart=on-success
和。RestartSec=10
[Service]
例如,考慮服務文件:/etc/systemd/system/vncserver@:2.service
.因此該文件將如下所示:
[單元] 描述=遠端桌面服務(VNC) After=syslog.target 網路.target [服務] 類型=分叉 ExecStartPre=+/usr/libexec/vncsession-restore %i ExecStart=/usr/libexec/vncsession-start %i PIDFile=/run/vncsession-%i.pid 重新啟動=成功 重新啟動秒=10 SELinuxContext=system_u:system_r:vnc_session_t:s0 [安裝] WantedBy=多用戶.target
然後運行systemctl daemon-reload
並systemctl restart vncserver@:2
.此後,即使登出 vnc 會話,您也應該能夠重新登入。