服務登出後也能自啟的改造過程-(Linux服務)

babyfengfjx發表於2024-04-24

背景

之前手動編寫了一個X11VNC的服務,方便遠端連線我的linux桌面環境,但發現每次登出後這個vnc服務就停掉了,沒有自己起來,只有重啟開機才能正常啟動,於是就想著需要改造一下。

原版服務內容

[Unit]
Description=Remote desktop service (VNC)
Requires=display-manager.service
After=display-manager.service

[Service]
Type=simple
ExecStart=/usr/bin/x11vnc -auth guess -forever -rfbauth /etc/x11vnc.pass -rfbport 5900 -shared
ExecStop=/usr/bin/killall x11vnc

[Install]
WantedBy=multi-user.target

但上面這個服務是沒法在登出後重啟服務的。

改造版本

[Unit]
Description=Remote desktop service (VNC)
Requires=display-manager.service
After=display-manager.service

[Service]
Type=forking
ExecStart=/usr/bin/x11vnc -auth guess -forever -rfbauth /etc/x11vnc.pass -rfbport 5900 -shared
ExecStop=/usr/bin/killall x11vnc
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target

這裡主要做了兩個更改:

  • 將服務型別(Type)從 "simple" 改為 "forking"。這樣,服務將作為一個守護程序執行,並且 systemd 將等待該程序建立子程序後認為服務已經啟動完成。

  • 新增了兩行新設定:

Restart=always:這將指示 systemd 在服務意外退出時自動重新啟動它。
RestartSec=3:這將設定在重新啟動服務之前等待的時間間隔(以秒為單位)。在這個例子中,設定為 3 秒。
儲存更改後,重新載入 systemd 配置並重新啟動服務:

透過日誌驗證登出後的重啟

journalctl -fu x11vnc.service

上述命令可以檢視服務的實時日誌資訊。

  • -f 是實時檢視日誌的引數
  • -u 後面接服務名稱,就是檢視該服務的日誌資訊

透過下面的日誌可以看到登出後服務能正常自啟了

deepin@deepin-PC:~$ sudo journalctl -fu x11vnc.service
Verify your face or password:
驗證成功
Apr 24 11:43:15 deepin-PC x11vnc[11194]: 24/04/2024 11:43:15
Apr 24 11:43:15 deepin-PC x11vnc[11194]: The VNC desktop is:      deepin-PC:0
Apr 24 11:43:15 deepin-PC x11vnc[11194]: PORT=5900
Apr 24 11:43:15 deepin-PC x11vnc[11194]: ******************************************************************************
Apr 24 11:43:15 deepin-PC x11vnc[11194]: Have you tried the x11vnc '-ncache' VNC client-side pixel caching feature yet?
Apr 24 11:43:15 deepin-PC x11vnc[11194]: The scheme stores pixel data offscreen on the VNC viewer side for faster
Apr 24 11:43:15 deepin-PC x11vnc[11194]: retrieval.  It should work with any VNC viewer.  Try it by running:
Apr 24 11:43:15 deepin-PC x11vnc[11194]:     x11vnc -ncache 10 ...
Apr 24 11:43:15 deepin-PC x11vnc[11194]: One can also add -ncache_cr for smooth 'copyrect' window motion.
Apr 24 11:43:15 deepin-PC x11vnc[11194]: More info: http://www.karlrunge.com/x11vnc/faq.html#faq-client-caching
Apr 24 11:44:37 deepin-PC x11vnc[11194]: caught XIO error:
Apr 24 11:44:37 deepin-PC x11vnc[11194]: 24/04/2024 11:44:37 deleted 79 tile_row polling images.
Apr 24 11:44:37 deepin-PC systemd[1]: x11vnc.service: Control process exited, code=exited, status=3/NOTIMPLEMENTED
Apr 24 11:44:37 deepin-PC systemd[1]: x11vnc.service: Failed with result 'exit-code'.
Apr 24 11:44:37 deepin-PC systemd[1]: Failed to start x11vnc.service - Remote desktop service (VNC).
Apr 24 11:44:40 deepin-PC systemd[1]: x11vnc.service: Scheduled restart job, restart counter is at 19.
Apr 24 11:44:40 deepin-PC systemd[1]: Starting x11vnc.service - Remote desktop service (VNC)...
Apr 24 11:44:40 deepin-PC x11vnc[11895]: 24/04/2024 11:44:40 passing arg to libvncserver: -rfbauth
Apr 24 11:44:40 deepin-PC x11vnc[11895]: 24/04/2024 11:44:40 passing arg to libvncserver: /etc/x11vnc.pass
Apr 24 11:44:40 deepin-PC x11vnc[11895]: 24/04/2024 11:44:40 passing arg to libvncserver: -rfbport
Apr 24 11:44:40 deepin-PC x11vnc[11895]: 24/04/2024 11:44:40 passing arg to libvncserver: 5900
Apr 24 11:44:40 deepin-PC x11vnc[11895]: 24/04/2024 11:44:40 x11vnc version: 0.9.16 lastmod: 2019-01-05  pid: 11895
Apr 24 11:44:40 deepin-PC x11vnc[11939]: xauth:  unable to generate an authority file name
Apr 24 11:44:40 deepin-PC x11vnc[11895]: 24/04/2024 11:44:40 -auth guess: using 'XAUTHORITY=/var/run/lightdm/root/:0' for disp='unset'
Apr 24 11:44:40 deepin-PC x11vnc[11895]: 24/04/2024 11:44:40 XOpenDisplay("") failed.
Apr 24 11:44:40 deepin-PC x11vnc[11895]: 24/04/2024 11:44:40 Trying again with XAUTHLOCALHOSTNAME=localhost ...
Apr 24 11:44:40 deepin-PC x11vnc[11895]: [21B blob data]
Apr 24 11:44:40 deepin-PC x11vnc[11895]: 24/04/2024 11:44:40 *** XOpenDisplay failed. No -display or DISPLAY.
Apr 24 11:44:40 deepin-PC x11vnc[11895]: 24/04/2024 11:44:40 *** Trying ":0" in 4 seconds.  Press Ctrl-C to abort.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:40 *** 1 2 3 4
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 *** XOpenDisplay of ":0" successful.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 Using X display :0
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 rootwin: 0x653 reswin: 0xc00001 dpy: 0xe07eded0
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 ------------------ USEFUL INFORMATION ------------------
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 X DAMAGE available on display, using it for polling hints.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   To disable this behavior use: '-noxdamage'
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   Most compositing window managers like 'compiz' or 'beryl'
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   cause X DAMAGE to fail, and so you may not see any screen
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   updates via VNC.  Either disable 'compiz' (recommended) or
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   supply the x11vnc '-noxdamage' command line option.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 Wireframing: -wireframe mode is in effect for window moves.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   If this yields undesired behavior (poor response, painting
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   errors, etc) it may be disabled:
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44    - use '-nowf' to disable wireframing completely.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44    - use '-nowcr' to disable the Copy Rectangle after the
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44      moved window is released in the new position.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   Also see the -help entry for tuning parameters.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   You can press 3 Alt_L's (Left "Alt" key) in a row to
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   repaint the screen, also see the -fixscreen option for
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   periodic repaints.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 XFIXES available on display, resetting cursor mode
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   to: '-cursor most'.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   to disable this behavior use: '-cursor arrow'
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   or '-noxfixes'.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 using XFIXES for cursor drawing.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 GrabServer control via XTEST.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 Scroll Detection: -scrollcopyrect mode is in effect to
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   use RECORD extension to try to detect scrolling windows
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   (induced by either user keystroke or mouse input).
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   If this yields undesired behavior (poor response, painting
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   errors, etc) it may be disabled via: '-noscr'
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   Also see the -help entry for tuning parameters.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   You can press 3 Alt_L's (Left "Alt" key) in a row to
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   repaint the screen, also see the -fixscreen option for
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   periodic repaints.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 XKEYBOARD: number of keysyms per keycode 7 is greater
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   than 4 and 51 keysyms are mapped above 4.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   Automatically switching to -xkb mode.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   If this makes the key mapping worse you can
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   disable it with the "-noxkb" option.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44   Also, remember "-remap DEAD" for accenting characters.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 X FBPM extension not supported.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 X display is capable of DPMS.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 --------------------------------------------------------
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 Default visual ID: 0x21
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 Read initial data from X display into framebuffer.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 initialize_screen: fb_depth/fb_bpp/fb_Bpl 24/32/10080
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 X display :0 is 32bpp depth=24 true color
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 Listening for VNC connections on TCP port 5900
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 Listening for VNC connections on TCP6 port 5900
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 listen6: bind: Address already in use
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 Not listening on IPv6 interface.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 Xinerama is present and active (e.g. multi-head).
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 Xinerama: number of sub-screens: 1
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 Xinerama: no blackouts needed (only one sub-screen)
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 fb read rate: 897 MB/sec
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 fast read: reset -wait  ms to: 10
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 fast read: reset -defer ms to: 10
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 The X server says there are 10 mouse buttons.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44 screen setup finished.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: 24/04/2024 11:44:44
Apr 24 11:44:44 deepin-PC x11vnc[11895]: The VNC desktop is:      deepin-PC:0
Apr 24 11:44:44 deepin-PC x11vnc[11895]: PORT=5900
Apr 24 11:44:44 deepin-PC x11vnc[11895]: ******************************************************************************
Apr 24 11:44:44 deepin-PC x11vnc[11895]: Have you tried the x11vnc '-ncache' VNC client-side pixel caching feature yet?
Apr 24 11:44:44 deepin-PC x11vnc[11895]: The scheme stores pixel data offscreen on the VNC viewer side for faster
Apr 24 11:44:44 deepin-PC x11vnc[11895]: retrieval.  It should work with any VNC viewer.  Try it by running:
Apr 24 11:44:44 deepin-PC x11vnc[11895]:     x11vnc -ncache 10 ...
Apr 24 11:44:44 deepin-PC x11vnc[11895]: One can also add -ncache_cr for smooth 'copyrect' window motion.
Apr 24 11:44:44 deepin-PC x11vnc[11895]: More info: http://www.karlrunge.com/x11vnc/faq.html#faq-client-caching

相關文章