赞
踩
1、Python程序开机自启
2、Ubuntu Linux 20.04 LTS 版本
3、Conda管理虚拟环境
在目录“/home/{username}/my_python_app/”下,创建run.sh脚本,内容为:
- #!/bin/bash
-
- # activate conda env
- source /home/{username}/anaconda3/bin/activate your_env
-
- # go to target path
- cd /home/{username}/program/
-
- # run python script
- python app.py
在目录“/etc/systemd/system”下创建一个脚本,例如my_app.service,内容为:
[Unit] Description=my python app After=network.target [Service] ExecStart=/home/{username}/my_python_app/run.sh User=root Group=root Restart=always [Install] WantedBy=multi-user.target
After 代表要在什么服务启动后再启动此服务;
ExecStart 是要运行的脚本的路径;
其它几项都设置成 root 代表使用 root 账户运行此服务;
开机自启设置需要使用命令systemctl。常见用法如下:
刷新
systemctl daemon-reload
设置开机自启动
systemctl enable my_app.service
启动
systemctl start my_app.service
关闭开机自启动
sudo systemctl disable my_app.service
查看状态
sudo systemctl status my_app.service
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。