422 字
2 分钟
Ubuntu 设置开机自启动项
前言
介绍两种不同的Ubuntu自启动的方法和它们的推荐食用方法。
如果你希望设置的是 带有GUI的应用,我推荐是使用方法1(gnome-session-properties)。
如果你希望设置的是 ftp、sshd等服务,推荐使用方法2(rc-local.service)。
系统:Ubuntu 20.04
方法1:通过gnome-session设置自启动项
通过该方法设置的自启动项,它们的启动时间是在你登录图形化界面之后。
在终端中输入 gnome-session-properties 设置桌面开启时需要启动的应用程序:
增加方式也非常简单,重要的是命令一栏,在里面添加你在终端中启动该应用所需要的命令即可。
方法2:通过rc-local.service服务实现自启动项
通过这个方法,如果是在默认的启动优先级下,都是系统开启的同时就开始运行了,如果启动带有GUI的应用会有如下报错:Error: no DISPLAY environment variable specified。
首先,修改 /lib/systemd/system/rc-local.service,增加 Install
项:
sudo vim /lib/systemd/system/rc-local.service
## This file is part of systemd.## systemd is free software; you can redistribute it and/or modify it# under the terms of the GNU Lesser General Public License as published by# the Free Software Foundation; either version 2.1 of the License, or# (at your option) any later version.
# This unit gets pulled automatically into multi-user.target by# systemd-rc-local-generator if /etc/rc.local is executable.[Unit]Description=/etc/rc.local CompatibilityDocumentation=man:systemd-rc-local-generator(8)ConditionFileIsExecutable=/etc/rc.localAfter=network.target
[Service]Type=forkingExecStart=/etc/rc.local startTimeoutSec=0RemainAfterExit=yesGuessMainPID=no
[Install]WantedBy=multi-user.targetAlias=rc-local.service
然后,新建 /etc/rc.local
脚本,配置自启动项:
sudo vim /etc/rc.local
#!/bin/shecho "自启动脚本运行成功" > /user/local/test.logexit 0
赋予 /etc/rc.local
可执行权限:
sudo chmod +x /etc/rc.local
然后就可以该启动 rc-local.service 服务了:
sudo systemctl start rc-local.service # 启动 rc-local.servicesudo systemctl status rc-local.service # 查看 rc-local.service 的状态
Ubuntu 设置开机自启动项
https://fuwari.vercel.app/posts/ubuntu/ubuntu-设置开机自启动项/