ubuntu20.04 开机自启动配置

1、使用root权限操作,因为开机自启动一般是在root用户运行,所以对应的环境也要在root下

2、在文件 /usr/lib/systemd/system/rc-local.service下面添加如下内容,并且修改ExecStart

[Service]
ExecStart=/etc/rc.local start
[Install]  
WantedBy=multi-user.target  
Alias=rc-local.service

2、配置 rc-local 服务

sudo cp /usr/lib/systemd/system/rc-local.service   /etc/systemd/system/            #复制 rc-local.service 文件

3、新建 rc.local 文件

sudo touch /etc/rc.local
sudo chmod 777 /etc/rc.local

4、在 rc.local 文件中加入自己想要开机自启的程序

#!/bin/bash -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other value on error.
#
# In order to enable or disable this script just change the execution bits.
#
# By default this script does nothing.
  
# /mnt/tss/monitor.sh
#/home/tcwg/agent_tmp/wsssr_install/install -c 10.168.43.13 -U aeac1528f6654448a4f9b9aeb84655be -d

cd /mnt/tss && ./monitor-root.sh 
#su tcwg -c "/bin/bash /mnt/tss/monitor.sh"	#临时使用tcwg用户运行文件

exit 0

5、设置开机自启动

systemctl enable rc-local
systemctl start rc-local.service  #启动服务
systemctl stop rc-local.service  #暂停服务
systemctl status rc-local.service #查看状态
systemctl daemon-reload # 重新加载自启服务

运行中遇到的bug:

1.The unit files have no installation config (WantedBy, RequiredBy, Also, Alias settings in the [Install] section, and DefaultInstance for template units). This means they are not meant to be enabled using systemctl.

ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/

在文件 rc-local.service 下面添加如下内容,并且修改ExecStart

[Service]
ExecStart=/etc/rc.local start
[Install]  
WantedBy=multi-user.target  
Alias=rc-local.service
systemctl enable rc-local
systemctl daemon-reload # 重新加载自启服务

2.开机自启动脚本可以正常运行,但是启动不了服务
nohup: failed to run command ‘python’: No such file or directory
nohup: failed to run command ‘gunicorn’: No such file or directory

sudo ln -s /opt/miniconda3/envs/tss/bin/python3.7 /usr/bin/python
sudo ln -s /usr/local/miniconda3/envs/tss/bin/gunicorn /usr/bin/gunicorn
### 设置应用程序或服务开机启动 在 Ubuntu 20.04 中,可以利用 `systemd` 来管理服务的自动启动。对于希望在系统引导时启动的服务或者应用,通常的做法是通过 `systemctl` 命令来实现这一目标。 为了使某个特定的服务能够在每次重启之后自动运行,在终端输入如下命令: ```bash sudo systemctl enable <service-name> ``` 这条指令会创建必要的符号链接,从而让指定的服务可以在系统初始化阶段被加载并启动[^5]。 如果涉及到的是自定义脚本而非标准服务,则应该先将其注册为 systemd 的单元文件。这一般意味着要编写一个 `.service` 文件放置于 `/etc/systemd/system/` 目录下,并确保该文件包含了正确的 `[Unit]`, `[Service]` 和 `[Install]` 部分描述信息。完成这些操作以后同样可以通过上述提到的方式激活它以便随机器一起启动。 另外值得注意的一点是在某些情况下可能还需要调整系统的默认目标(target),即改变进入图形界面之前的状态;不过对于大多数桌面环境来说,默认的目标已经设定好了,因此不需要额外处理这部分内容。 当需要取消某项服务的开机自启功能时,只需替换掉前面使用的动词即可: ```bash sudo systemctl disable <service-name> ``` 这样就可以轻松控制哪些程序和服务会在计算机开启的时候被执行了。 #### 示例:启用 Zabbix Server 开机启动 假设现在有一个名为 `zabbix-server` 的监控平台想要设置成随着操作系统一同启动的例子,那么具体做法就是执行下面给出的操作: ```bash sudo systemctl enable zabbix-server ``` 这将会使得 Zabbix server 成功加入到系统的启动序列当中去。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值