在ubuntu操作系统上部署服务并自启保活

以python + django + gunicorn + ubuntu20 为例

首先python环境用conda,查看虚拟环境的目录
conda env list
找到python的路径

  1. 写一个启动服务器的脚本 start.sh
#!/bin/bash
source /home/ema/app/miniconda/bin/activate subsys   # 替换为 conda 的实际路径
cd /home/xx/xx/subsystem # 切换到 项目配置目录,此目录下有 gunicorn的配置文件gunicorn_conf.py
gunicorn -c gunicorn_conf.py application.asgi:application # 
  1. 写一个服务
    sudo vim /etc/systemd/system/my-service.service
    因为 Ubuntu的系统服务放在 /etc/systemd/system这个目录下,创建的服务文件以service结尾
[Unit]
Description=subsystem App
After=network.target

[Service]
Type=simple
ExecStart=/bin/bash -c "/home/xx/xx/start.sh" # 启动脚本的位置名称
Restart=always   # 保活
RestartSec=3    # 3s一重启

[Install]
WantedBy=multi-user.target

保存并关闭文件.
3. 重新加载服务配置文件

sudo systemctl daemon-reload

  1. 启用自启动服务
    sudo systemctl enable my-service

  2. 启动服务

sudo systemctl start my-service
  1. 现在,你的服务应该已经成功创建并启动了。如果你希望停止服务,可以使用以下命令:
sudo systemctl stop my-service

如果你希望查看服务的状态,可以使用以下命令:

sudo systemctl status my-service

查看服务的启用状态

systemctl is-enabled 服务名

你可能感兴趣的:(ubuntu,linux,运维)