1、概述
测试环境:ubuntu22.04 带图形界面
实现方式1:通过图形界面的【启动应用程序】设置开机自启动;
实现方式2:通过配置为服务实现开机自启动。
2、图形界面设置
优点:
- 图形界面
- 使用简单
缺点:
- 灵活性差,如果启动有依赖或者需要实现守护进程只能通过写shell脚本实现。
找到【启动应用程序】;
点击【添加】,然后在【命令】栏输入需要启动的可执行程序路径,名称随便填。
点击【保存】后,在系统启动后会自动启动添加的可执行程序。
配置文件保存在~/.config/autostart
文件夹下。
3、设置为systemd服务
优点:
- 功能强大,使用灵活;
- 支持设置程序启动依赖,例如在网络准备好了后再启动;
缺点:
- 没有图形界面,使用较为复杂;
进入/etc/systemd/system
文件夹;
创建一个.service
后缀的文件,名称自定;
文件中填入启动服务的配置信息,如下所示;
[unit] description=webserver # 简单描述服务 after=network.target # 指定服务模块启动后再启动(网络模块启动) [service] user=mhf # 设置服务运行的用户,带图形界面的程序最好使用普通用户运行,否则可能会启动失败,自己系统的用户名 workingdirectory=/home/mhf/code/bin64 # 设置进程的工作目录 execstart=/home/mhf/code/bin64/chatserver # 设置启动的可执行程序路径 restart=always # 设置进程 退出后的重启方式 always:总是重启 restartsec=5 # 重启服务之前需要等待的秒数 environment=display=:0 # 带图形界面的程序必须指定运行桌面环境,否则会启动失败 # 环境变量 display 告诉gui程序如何与gui通信。unix系统可以运行多个x服务器,即多个显示。这些显示可以是物理显示(一个或多个监视器),或远程显示(通过网络转发,例如通过ssh),或虚拟显示,如xvfb等。指定显示的基本语法是 host:number ;如果忽略 host 部分,则显示为本地显示。 # 显示从0开始编号,因此 :0 是启动的第一个本地显示。在典型的设置中,这是显示在计算机显示器上的内容。 [install] wantedby=multi-user.target
- 常用配置说明
[unit] # 启动顺序与依赖关系。 description:对当前服务的简单描述。 after:指定.serive在哪些服务之后进行启动; before:指定.serive在哪些服务之前进行启动; 除上述内容,文件中还可能出现以下内容: requires:指定服务依赖于哪些服务(强依赖关系,一旦所依赖服务异常,当前服务也随之停止); wants:指定服务依赖于哪些服务(弱依赖关系,所依赖服务异常不影响当前服务正常运行)。 [service] # 启动行为 type:定义启动类型。可设置:simple,exec,forking,oneshot,dbus,notify,idle。 simple:execstart 字段启动的进程为该服务的主进程; forking:execstart 字段的命令将以 fork() 方式启动,此时父进程将会退出,子进程将成为主进程; execstart:定义启动进程时执行的命令; execstop:停止服务时执行的命令; 除上述内容外,文件中还可能出现: environmentfile:环境配置文件,用来指定当前服务启动的环境变量; execreload:重启服务时执行的命令; execstartpre:启动服务之前执行的命令; execstartpost:启动服务之后执行的命令; execstoppost:停止服务之后执行的命令; remainafterexit:设为yes,表示进程退出以后,服务仍然保持执行; restartsec:重启服务之前需要等待的秒数。 killmode:定义 systemd 如何停止服务,可以设置的值如下: control-group(默认值):当前控制组里面的所有子进程,都会被杀掉; process:只杀主进程; mixed:主进程将收到 sigterm 信号,子进程收到 sigkill 信号; none:没有进程会被杀掉。 restart:定义了退出后,systemd 的重启方式,可以设置的值如下: no(默认值):退出后不会重启; on-success:当进程正常退出时(退出状态码为0),才会重启; on-failure:当进程非正常退出时(退出状态码非0),包括被信号终止和超时,才会重启; on-abnormal:当被信号终止和超时,才会重启; on-abort:当收到没有捕捉到的信号终止时,才会重启; on-watchdog:看门狗超时退出,才会重启; always:总是重启。 [install] # 区块 install一般填为wantedby=multi-user.target,表示多用户环境下服务被启用。
- 配置完成后加载服务配置,用于服务文件修改后的配置更新。
sudo systemctl daemon-reload
- 立即启动服务
sudo systemctl start <service_name> # 例如 sudo systemctl start chatserver.service sudo systemctl start chatserver
- 停止服务
sudo systemctl stop <service_name> # 例如 sudo systemctl stop chatserver.service
错误信息 :如果gui程序没设置使用普通用户启动、没有设置environment=display=:0
就会报错
11月 25 17:48:30 mhf-virtual-machine systemd[1]: stopped webserver. 11月 25 17:48:30 mhf-virtual-machine systemd[1]: started webserver. 11月 25 17:48:30 mhf-virtual-machine chatserver[5154]: authorization required, but no authorization protocol specified 11月 25 17:48:30 mhf-virtual-machine chatserver[5154]: qt.qpa.xcb: could not connect to display :0 11月 25 17:48:30 mhf-virtual-machine chatserver[5154]: qt.qpa.plugin: could not load the qt platform plugin "xcb" in "" even though it was found. 11月 25 17:48:30 mhf-virtual-machine chatserver[5154]: this application failed to start because no qt platform plugin could be initialized. reinstalling the application may fix this problem. 11月 25 17:48:30 mhf-virtual-machine chatserver[5154]: available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl, xcb. 11月 25 17:48:30 mhf-virtual-machine systemd[1]: chatserver. service: main process exited, code=dumped, status=6/abrt 11月 25 17:48:30 mhf-virtual-machine systemd[1]: chatserver. service: failed with result 'core-dump'.
以上就是ubuntu设置程序开机自启动的操作步骤的详细内容,更多关于ubuntu程序开机自启动的资料请关注代码网其它相关文章!
发表评论