Ubuntu20.04开机自启动服务(以Python项目为例)

0. 应用背景

在一些AI项目中,往往通过接口对外提供服务,常用Docker来部署。在一些需要CUDA的场景,尽管有nvidia-docker容器可以使用,但配置过程相当繁琐。
可替换的方案(前提是已安装nvidia驱动、cuda+cudnn、conda并创建了运行环境):

  • 采用conda对服务的运行环境进行隔离
  • 布置开机自启动任务来后台执行代码
    接下来,将重点介绍开机自启动服务,以一个Python项目为例。

1. 查看启动脚本,其中就有我们需要的 rc.local.service

ls /lib/systemd/system

2. 打开rc-local.service脚本

可采用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两个英文跟上面的都是绿色的,要是绿色才有用。

3. 编辑/etc/rc.local文件

进入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

4. 创建软链接

systemd 默认读取 /etc/systemd/system 下的配置文件, 所以还需要在 /etc/systemd/system 目录下创建软链接:

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

5. 验证是否成功

重启,在终端输入:

service service-name status

查看服务是否正在运行。
如果在 /etc/rc.local 中添加的是 ./xxx.sh 这种类型,或者是一个可执行程序(如本案中run.py),要在末尾加上“&”, 不然重启ubuntu的时候会卡在启动界面进不去系统。
万一如此,以下链接参考救砖。
致谢:Ubuntu20.04 开机自启动程序

  • 0
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值