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