赞
踩
一、利用shell脚本启动conda虚拟环境
1.创建一个run.sh文件,将.bashrc文件中的conda 相关内容复制到sh文件中,具体内容如下:
(.bashrc文件在root下的home/自己的用户下)
从# >>> conda init >>>开始到# >>> conda init >>>结束
# added by Anaconda3 5.3.1 installer # >>> conda init >>> # !! Contents within this block are managed by 'conda init' !! __conda_setup="$(CONDA_REPORT_ERRORS=false '/home/yang/Anaconda/enter/bin/conda' shell.bash hook 2> /dev/null)" if [ $? -eq 0 ]; then \eval "$__conda_setup" else if [ -f "/home/yang/Anaconda/enter/etc/profile.d/conda.sh" ]; then . "/home/yang/Anaconda/enter/etc/profile.d/conda.sh" CONDA_CHANGEPS1=false conda activate base else \export PATH="/home/yang/Anaconda/enter/bin:$PATH" fi fi unset __conda_setup # <<< conda init <<<
#!/bin/bash # >>> conda init >>> # !! Contents within this block are managed by 'conda init' !! __conda_setup="$(CONDA_REPORT_ERRORS=false '/home/yang/Anaconda/enter/bin/conda' shell.bash hook 2> /dev/null)" if [ $? -eq 0 ]; then \eval "$__conda_setup" else if [ -f "/home/yang/Anaconda/enter/etc/profile.d/conda.sh" ]; then . "/home/yang/Anaconda/enter/etc/profile.d/conda.sh" CONDA_CHANGEPS1=false conda activate base else \export PATH="/home/yang/Anaconda/enter/bin:$PATH" fi fi unset __conda_setup # conda启动虚拟环境 conda activate deeplearn # 在指定的虚拟环境中启动特定脚本 cd /home/yang/DL_YOLO/pythonProject python testmain.py exit 0
二、设置开机自启动
1.假如需要开机自启动的程序是一个简单的保存程序 testmain.py
# 配置输出文件的路径。
path = '/home/yang/DL_YOLO/pythonProject/data/a.txt'
# 打开文件
f = open(path, "w+")
# 文件内写入“文本内容”四个字
f.write('保存内容')
# 关闭文件
f.close()
2.检查系统目录/lib/systemd/system/rc-local.service,如果没有自己新建,在文中增加[Install]项的。
新建文件
sudo vi /etc/systemd/system/rc-local.service
# 文件中本身就有的 [Unit] Description=/etc/rc.local Compatibility Documentation=man:systemd-rc-local-generator(8) ConditionFileIsExecutable=/etc/rc.local After=network.target [Service] Type=forking ExecStart=/etc/rc.local start TimeoutSec=0 #推迟时间 RemainAfterExit=yes GuessMainPID=no # 需要自己添加 [Install] WantedBy=multi-user.target Alias=rc-local.service
同样检查/etc/systemd/system/rc-local.service下面的内容,如上修改。
3. 创建rc.local文件
sudo vi /etc/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.
echo "看到这行字,说明添加自启动脚本成功。">/usr/local/test.log
#后台启动程序
bash /home/yang/run.sh
exit 0
sudo chmod +x /etc/rc.local
sudo systemctl enable rc-local
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service
7.查看效果
可以看到/usr/local/test.log文件被创建了,同时查看/home/yang/DL_YOLO/pythonProject/data/a.txt是否文件保存。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。