赞
踩
在一些AI项目中,往往通过接口对外提供服务,常用Docker来部署。在一些需要CUDA的场景,尽管有nvidia-docker容器可以使用,但配置过程相当繁琐。
可替换的方案(前提是已安装nvidia驱动、cuda+cudnn、conda并创建了运行环境):
ls /lib/systemd/system
可采用vi打开:
sudo vi /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 Compatibility ConditionFileIsExecutable=/etc/rc.local After=network.target [Service] Type=forking ExecStart=/etc/rc.local start TimeoutSec=0 RemainAfterExit=yes
一般正常的启动文件主要分成三部分
[Unit] 段: 启动顺序与依赖关系
[Service] 段: 启动行为,如何启动,启动类型
[Install] 段: 定义如何安装这个配置文件,即怎样做到开机启动
可以看出,/etc/rc.local 的启动顺序是在网络后面,但是显然它少了 Install 段,也就没有定义如何做到开机启动,所以显然这样配置是无效的。 因此我们就需要在后面帮他加上 [Install] 段:
[Install]
WantedBy=multi-user.target
Alias=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 Compatibility ConditionFileIsExecutable=/etc/rc.local After=network.target [Service] Type=forking ExecStart=/etc/rc.local start TimeoutSec=0 RemainAfterExit=yes [Install] WantedBy=multi-user.target Alias=rc-local.service
PS:添加了[Install]内容后,下面两行的WantedBy和Alias两个英文跟上面的都是绿色的,要是绿色才有用。
进入etc文件夹,查看有没有 /etc/rc.local 这个文件,没有则自己创建一个。
sudo vi /etc/rc.local
然后,把需要启动脚本写入 /etc/rc.local,此处以一个Python项目代码为例。
#!/bin/bash
source activate my_conda_env
cd /home/user/projects/my_python_project/
python3 run.py&
exit 0
注意:此处“#!/bin/sh”修改为“#!/bin/bash”,以执行source指令来激活conda运行环境。【此行为必须的!】
执行程序后必须加“&”!
以上过程依次进行了激活项目运行的conda环境、进入项目文件夹、执行运行程序。
然后给rc.local添加权限:
sudo chmod +x /etc/rc.local
systemd 默认读取 /etc/systemd/system 下的配置文件, 所以还需要在 /etc/systemd/system 目录下创建软链接:
sudo ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/
重启,在终端输入:
service service-name status
查看服务是否正在运行。
如果在 /etc/rc.local 中添加的是 ./xxx.sh 这种类型,或者是一个可执行程序(如本案中run.py),要在末尾加上“&”, 不然重启ubuntu的时候会卡在启动界面进不去系统。
万一如此,以下链接参考救砖。
致谢:Ubuntu20.04 开机自启动程序
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。