ubuntu22.04上设定Service程序自启动,自动运行Conda环境下的Python脚本(亲测)

一、项目需求:

1、在Ubuntu Linux 22.04 LTS 版本的系统是部署开机自启动service;

2、通过widows11与Ubuntu Linux 22.04进行Post请求;

3、启动python的服务,在指定的conda环境下运行代码。

二、实施流程:

1、创建运行脚本

bash 复制代码
#在目标目录创建一个脚本

sudo vim auto_run.sh

2、自动执行内容

然后在auto_rune.sh中贴入下面的内容:Anaconda 安装目录及 activate 脚本的具体目录位置,需要替换成你的安装路径;之后切换到你的项目的根目录;

使用 Python 指令运行目标 Python 程序;

python 复制代码
#!/bin/bash

# activate conda env
source /home/user/anaconda3/bin/activate fastapi

# use target path
cd /home/user/Python/code/

python FastAPI.py

3、建立一个Service

bash 复制代码
#在目标目录创建一个 Service 文件

sudo vim auto_run.service

#贴入下面的内容:

[Unit]

Description=Run FastAPI service at Startup

After=network.target

[Service]

ExecStart=/home/user/Python/code/auto_run.sh
User=root
Group=root
Restart=always

[Install]

WantedBy=multi-user.target

#After 代表要在什么服务启动后再启动此服务;
#ExecStart 是要运行的脚本的路径;
#其它几项都设置成 root 代表使用 root 账户运行此服务;

#保存后,需要将其移动到系统服务的文件夹内。

sudo cp auto_run.service /usr/lib/systemd/system/auto_run.service

4、执行以下步骤

bash 复制代码
#然后刷新

sudo systemctl daemon-reload

#启动

sudo systemctl start auto_run.service

#设置开机自启动(可选)

sudo systemctl enable auto_run.service

#关闭开机自启动(可选)

sudo systemctl disable auto_run.service

#查看状态

sudo systemctl status auto_run.service
相关推荐
AnalogElectronic1 小时前
人工智能初级工程师认证复习纲要(高频重点标记)
人工智能
前端不太难1 小时前
AI 原生架构:鸿蒙App的下一代形态
人工智能·架构·harmonyos
Fzuim1 小时前
从 LLM 接口到 Agent 接口:AI 融合系统的架构演进与未来趋势分析报告
人工智能·ai·重构·架构·agent·runtime
毕设源码-邱学长6 小时前
【开题答辩全过程】以 基于Java的学校住宿管理系统的设计与实现为例,包含答辩的问题和答案
java·开发语言
rookieﻬ°7 小时前
PHP框架漏洞
开发语言·php
GISer_Jing7 小时前
AI自动化工作流:智能驱动未来(升级研究生项目!!!)
人工智能·前端框架·自动化
草捏子7 小时前
Agent Skills:让 AI 一次学会、永远记住的能力扩展方案
人工智能
NocoBase7 小时前
【2.0 教程】第 1 章:认识 NocoBase ,5 分钟跑起来
数据库·人工智能·开源·github·无代码
后端小肥肠7 小时前
OpenClaw实战|从识图到公众号内容自动化,我跑通了完整链路
人工智能·aigc·agent
猿界零零七8 小时前
pip install mxnet 报错解决方案
python·pip·mxnet