ubuntu18.04下,利用shell脚本启动conda虚拟环境,并实现python程序自启动

一、利用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 <<<
  1. 在上面的基础上,增加启动虚拟环境,如conda activate 虚拟环境名,和自己的启动内容,形成如下的sh文件。
#!/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
  1. 增加自己后台启动程序
#!/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
  1. 给rc.local添加权限
sudo chmod +x /etc/rc.local
  1. 启动服务
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是否文件保存。

参考文献 1
参考文献2
参考文献3
参考文献0

  • 6
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值