记录一次将java前后端项目和使用了conda虚拟环境的python项目添加到ubuntu服务艰难历程
步骤
- 编写服务文件:
sudo nano /etc/systemd/system/run-ai_yujing.service
[Unit] Description=AI YuJing System Startup Script After=network.target [Service] Type=simple User=root Group=root WorkingDirectory=/home/user/ai_yujing Environment="PATH=/usr/local/cuda-12.6/bin:/home/user/anaconda3/envs/cv2h264/bin:/home/user/anaconda3/condabin:/usr/local/cuda-12.6/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin" ExecStart=/bin/bash -c 'source /home/user/anaconda3/etc/profile.d/conda.sh && conda activate cv2h264 && cd /home/user/ai_yujing && sudo -S ls <<< "你的user用户的密码" && nohup java -jar gx-admin.jar & cd /home/user/ai_yujing/video-detect && python flaskserver.py' Restart=always [Install] WantedBy=multi-user.target
- 其中
Environment
项可以在切换到对应conda虚拟环境后用printenv
命令查看
- 其中
- 启用服务开机自启:
sudo systemctl start run-ai_yujing.service
- 启动服务:
sudo systemctl enable run-ai_yujing.service
- 查看服务状态:
sudo systemctl status run-ai_yujing.service
- 查看服务日志:
sudo journalctl -u run-ai_yujing.service
- 时间倒序查看服务日志:
sudo journalctl -u run-ai_yujing.service -r
- 停止服务:
sudo systemctl stop run-ai_yujing.service
- 重启服务:
sudo systemctl restart run-ai_yujing.service
- 禁止服务开机自启:
sudo systemctl disable run-ai_yujing.service
问题
- 如果遇到这种权限问题:
at java.io.FileNotFoundException: /home/gx/logs/sys-info.log (Permission denied)
- 可以修改对应路径权限:
# 检查权限 ls -l /home/gx/logs # 给目标目录赋予适当权限 sudo chown -R user:user /home/gx/logs sudo chmod -R 775 /home/gx/logs