请教 Django runserver 开机自启动的问题

2024-07-23 09:39:05 +08:00
 hwhtj

/etc/systemd/system/rc-local.service 文件内容:

 Description=/etc/rc.local Compatibility
 ConditionPathExists=/etc/rc.local

[Service]
 Type=forking
 ExecStart=/etc/rc.local start
 TimeoutSec=0
 StandardOutput=tty
 RemainAfterExit=yes
 SysVStartPriority=99

[Install]
 WantedBy=multi-user.target

/etc/rc.local 文件内容

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
echo "看到这行字,说明添加自启动脚本成功。" > /home/some/Qexo/test.log
a=`lsof -i:8000 | wc -l`
if [ "$a" -eq "0" ];then
       echo "start nohup django:8000" >> /home/some/Qexo/test.log
             nohup  /usr/bin/python3 /home/some/Qexo/manage.py runserver 0.0.0.0:8000 > cmdb.log 2>&1 &
       else
       echo "8000 端口被占用" >> /home/some/Qexo/test.log
fi
#source/home/some/Qexo/
#nohup /usr/bin/python3 /home/some/Qexo/manage.py runserver 0.0.0.0:8000 > cmdb.log 2>&1 &
echo "看到这行字,说明 django.sh 执行过。" >> /home/some/Qexo/test.log
exit 0

重启后,sudo systemctl status rc-local,报如下错误

 rc-local.service - /etc/rc.local Compatibility
    Loaded: loaded (/etc/systemd/system/rc-local.service; enabled; preset: enabled)
   Drop-In: /usr/lib/systemd/system/rc-local.service.d
            └─debian.conf
    Active: failed (Result: exit-code) since Tue 2024-07-23 09:22:23 CST; 15min ago
  Duration: 236ms
   Process: 3617 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
  Main PID: 3621 (code=exited, status=1/FAILURE)
       CPU: 344ms

7 月 23 09:22:23 some-Ubuntu23 systemd[1]: Starting rc-local.service - /etc/rc.local Compatibility...
7 月 23 09:22:23 some-Ubuntu23 systemd[1]: Started rc-local.service - /etc/rc.local Compatibility.
7 月 23 09:22:23 some-Ubuntu23 systemd[1]: rc-local.service: Main process exited, code=exited, status=1/FAILURE
7 月 23 09:22:23 some-Ubuntu23 systemd[1]: rc-local.service: Failed with result 'exit-code'.
3183 次点击
所在节点    Django
14 条回复
byaiu
2024-07-23 09:42:39 +08:00
猜测是 pythonpath 的问题
依赖是不是全局装的?
而且不是有 log 么,看看 log 就好了
Vegetable
2024-07-23 09:44:18 +08:00
一方面,你至少把你的 test.log 放出来,免得大家肉眼检查脑内运行帮你看问题。
另一方面,你这个需求其实很适合将服务放在 dcoker 里边自动启动,全是好处。
lingeo
2024-07-23 09:46:09 +08:00
1.直接 bash 运行 rc.local 脚本试一下有没有报错。
2.推荐你使用 supervisor 管理进程。
Spute
2024-07-23 09:53:16 +08:00
1 、切到项目目录,以前台模式直接执行/usr/bin/python3 /home/some/Qexo/manage.py runserver 0.0.0.0:8000 ,进行 debug
2 、建议使用 docker 部署这种 web 服务器
dolphintwo
2024-07-23 10:31:03 +08:00
写一个 django 的 service 再 enable 它
lzZzeucJri6FcR0o
2024-07-23 10:50:22 +08:00
人才
hwhtj
2024-07-23 11:08:31 +08:00
我登录 的不是 root 用户。
直接 sudo bash 运行 rc.local 脚本 成功,
直接运行/usr/bin/python3 /home/some/Qexo/manage.py runserver 0.0.0.0:8000 ,也会成功,但就是把命令放在 rc.local 里面,再由 systemctl 调用就不成功
说到最后,我就是想在 ubuntu 启动的时候自动运行/usr/bin/python3 /home/some/Qexo/manage.py runserver 0.0.0.0:8000 --noreload 这么一句脚本,可试了很多方法都不行
hwhtj
2024-07-23 11:23:38 +08:00
hwhtj
2024-07-23 11:25:37 +08:00
pililink
2024-07-23 13:47:04 +08:00
推荐你使用 supervisor 管理进程
skyrim61
2024-07-24 08:47:03 +08:00
使用 gunicorn 部署
zcybupt2016
2024-07-24 14:30:41 +08:00
建议打 docker + 1 ,省事得多
elboble
2024-07-24 14:49:37 +08:00
supervisor +1 ,或者 unicron ,代拉服务。runserver 是调试用法
huangzhiyia
359 天前
分享一个自己生产环境常用的配置

1.不要用系统自带的 Python 而是转为使用 venv 之类的虚拟环境

我一个服务器上跑着不同版本的 Python 环境, 直接从 Debian 10 升级到 Debian 12 没有任何问题,如果用系统自带的 python 肯定 GG 思密达了

2.在服务器项目目录下执行 python -m venv venv 然后 source venv/bin/activate 激活环境后进行初始化之类的操作

pip install -r requirements.txt
python manage.py xxxxxxxxxxx

3. 最后配置 system service


```bash
[Unit]
Description=edge-system
After=network.target

[Service]
User=admin
Group=admin
CPUQuota=50%
MemoryLimit=1024M
WorkingDirectory=/usr/local/edgesystem
Restart=always
RestartSec=5
ExecStart=/usr/local/edgesystem/venv/bin/gunicorn conf.wsgi:application --workers 4 --threads 3 -b 127.0.0.1:8001
[Install]
WantedBy=multi-user.target
```

4.最终执行

systemctl --now enable servicename

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1059319

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX