首页 > 系统相关 >服务注销后也能自启的改造过程-(Linux服务)

服务注销后也能自启的改造过程-(Linux服务)

时间:2024-04-24 11:56:11浏览次数:20  
标签:24 11 服务 44 x11vnc deepin PC 自启 Linux

背景

之前手动编写了一个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

标签:24,11,服务,44,x11vnc,deepin,PC,自启,Linux
From: https://www.cnblogs.com/babyfengfjx/p/18154742

相关文章

  • linux crontab 如何创建,编辑,重启
    一.如何创建crontab定时任务:通常情况下,只有root用户和具有sudo权限的用户才能执行crontab此操作。1.添加/编辑crontab文件:如何添加/编辑Crontab:crontab-e#这将打开文本编辑器以编辑当前用户的cron作业。#默认情况下,系统会编辑当前登录用户的crontab命令集合。需要编辑......
  • 使用SSH从公网服务器简易使用内网任意机器服务,比如从外部下载代码
    如果有一个台外部的机器waibu-host,位于阿里云,腾讯云,亚马逊,azure云等等; 假设你想从这个waibu-host上访问公司任意服务,下载代码,访问http服务,等等,或者你机器上的服务,不需要额外工具,只要ssh反向就可以实现; 借个图,如下: 举个例子,如果你想从公司内部......
  • linux shell 脚本调试技巧
    本文分享自天翼云开发者社区《linuxshell脚本调试技巧》,作者:g****n1.bash-x加脚本这是最常用的调试手段,终端会打印出,脚本执行过程中的每一个命令。a.查看脚本流程有无错误b.查看变量值有无错误c.查看退出位置是否符合预期例如脚本 small_test.sh #!/bin/bashecho......
  • 服务器部署jar包执行命令编写
    服务器部署jar包执行命令编写新建deploy.sh文件,并编写内容SERVER_NAME=demo-server-8.2.0.15.jar#服务jar包名称#根据服务名称查询对应的pidpid=$(ps-ef|grep$SERVER_NAME|grep-vgrep|awk'{print$2}');#判断进程是否存在,存在则杀死if[-n"$pid"];thenkill......
  • 在Linux中,find命令和locate命令有什么区别?
    在Linux中,find和locate都是用于查找文件的命令,但它们的工作方式和效率有着显著的不同:1.find命令工作原理:find命令直接在文件系统中遍历目录结构,实时查找符合特定条件的文件或目录。这意味着它会逐个检查文件系统的实际内容,因此查找过程可能会比较慢,尤其是当搜索大型文件系统......
  • 在Linux中,如何使用grep命令搜索文本?
    grep是Linux系统中非常强大的文本搜索工具,它允许用户使用正则表达式搜索文本,并将匹配的行打印到标准输出。下面我将详细解释如何使用grep命令进行文本搜索。1.基本语法grep[OPTIONS]PATTERN[FILE...]PATTERN:要搜索的模式或正则表达式。FILE:要搜索的文件名。如果省......
  • 在Linux中,如何使用sed命令进行文本替换?
    在Linux中,sed(StreamEditor)是一个用于处理文本流的命令行工具,它非常适合用于执行基本的文本转换。sed可以读取输入的文本文件,根据指定的指令对文本进行处理,并将结果输出到标准输出设备。以下是如何使用sed命令进行文本替换的详细说明:1.基本语法sed命令的基本语法如下:sed's/re......
  • Linux用户与用户组管理
    Linux是一个多用户、多任务的分时操作系统,在Linux系统1中,用户的账号等相关信息(密码除外)均放在etc目录下文件所有者:Linux系统中的文件所有者指的是文件的拥有者用户组:Linux系统中的用户大体上可以分为三组:管理员(root)、普通用户和系统用户用户与用户组管理用户管理用户管......
  • 在Linux中,awk命令的如何使用?
    在Linux中,awk是一个强大的文本处理工具,它允许你按照模式扫描文件,并根据扫描结果执行相应的操作,比如打印、修改或过滤文件中的数据。awk的语法灵活,可以进行复杂的文本分析和数据处理。下面是使用awk命令的一些基本概念和常见用法:1.基本结构awk的基本命令格式如下:awk'pattern{......
  • 在Linux中, 如何查看系统的资源使用情况?
    在Linux系统中,查看资源使用情况对于系统管理员来说是一项常规任务,它有助于了解系统的性能和瓶颈。以下是一些常用的命令和方法来查看系统的资源使用情况:1.查看CPU使用情况top:显示实时的CPU使用情况,包括用户空间、系统空间、空闲和每个进程的CPU占用率。tophtop:top的增强......