iT邦幫忙

0

如何設定uwsgi 在開機時自啟載入執行

  • 分享至 

  • xImage

參考https://blog.gtwang.org/linux/auto-execute-linux-scripts-during-boot-login-logout/ Linux 開機自動執行指令

my_script.sh內容:

bash:
sudo service nginx restart
/usr/local/bin/uwsgi --emperor /home/ubuntu/uwsgi-sites/ --daemonize /home/ubuntu/logs/uwsgi-emperor.log
sudo service nginx restart
sudo killall -s INT /usr/local/bin/uwsgi
/usr/local/bin/uwsgi --emperor /home/ubuntu/uwsgi-sites/ --daemonize /home/ubuntu/logs/uwsgi-emperor.log

確認這個指令稿有執行權限:
chmod +x /opt/my_script.sh

在 /etc/rc.local 中加入這二行指令稿:

/opt/my_script.sh

exit 0

但ubuntu 重啟後,無法啟入載入uwsig,請問要如何在機器重啟時載入uwsgi?

註:
我用
pip install -U uwsgi
安裝uwsgi,uwsgi binary in /usr/local/bin/uwsgi

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
froce
iT邦大師 1 級 ‧ 2020-07-29 14:43:47
最佳解答

ubuntu現在應該是不能用rc.local來開機啟動。

目前較常用的是systemctl
這邊是我從實際用的uwsgi.service幫你改的

sudo nano /etc/systemd/system/uwsgi.service

[Unit]
Description=uWSGI Emperor service

[Service]
ExecStartPre=/bin/bash -c 'mkdir -p /run/uwsgi; chown www-data:www-data /run/uwsgi'
ExecStart=/usr/local/bin/uwsgi --emperor /home/ubuntu/uwsgi-sites/ --daemonize /home/ubuntu/logs/uwsgi-emperor.log
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target

編輯完之後用systemctl enable應該就能開機啟動了,不能的話檢查路徑、權限看看。

看更多先前的回應...收起先前的回應...
c3jack iT邦新手 5 級 ‧ 2020-07-30 15:49:48 檢舉

froce大師,感謝您的回答,但還是開不起來,請問要去那裹看error log?

c3jack iT邦新手 5 級 ‧ 2020-07-30 16:37:44 檢舉

我剛確定有建立 /run/uwsgi目錄,且chown也有作用。當我開機起來時,我只要打
uwsgi --emperor /home/ubuntu/uwsgi-sites/ --daemonize /home/ubuntu/logs/uwsgi-emperor.log
可啟動服務,等同於打
/usr/local/bin/uwsgi --emperor /home/ubuntu/uwsgi-sites/ --daemonize /home/ubuntu/logs/uwsgi-emperor.log
不知是否找不到uwsgi?

c3jack iT邦新手 5 級 ‧ 2020-07-30 16:42:20 檢舉

我打systemctl status uwsgi.service,得到下列錯誤訊息:
mkdir 和 uwsgi都有成功執行,但最後卻因protocol而失敗,有點搞不清楚,uwsgi有成功了但為何服務仍失敗

● uwsgi.service - uWSGI Emperor service
   Loaded: loaded (/etc/systemd/system/uwsgi.service; enabled; vendor preset: enabled)
   Active: failed (Result: protocol) since Thu 2020-07-30 08:30:01 UTC; 9min ago
  Process: 986 ExecStart=/usr/local/bin/uwsgi --emperor /home/ubuntu/uwsgi-sites/ --daemonize /home/ubuntu/logs/uwsgi-emperor.log (cod
  Process: 983 ExecStartPre=/bin/bash -c mkdir -p /run/uwsgi; chown www-data:www-data /run/uwsgi (code=exited, status=0/SUCCESS)
 Main PID: 986 (code=exited, status=0/SUCCESS)
   Status: "initializing uWSGI"

Jul 30 08:30:01 ip-172-31-43-23 systemd[1]: uwsgi.service: Service hold-off time over, scheduling restart.
Jul 30 08:30:01 ip-172-31-43-23 systemd[1]: uwsgi.service: Scheduled restart job, restart counter is at 5.
Jul 30 08:30:01 ip-172-31-43-23 systemd[1]: Stopped uWSGI Emperor service.
Jul 30 08:30:01 ip-172-31-43-23 systemd[1]: uwsgi.service: Start request repeated too quickly.
Jul 30 08:30:01 ip-172-31-43-23 systemd[1]: uwsgi.service: Failed with result 'protocol'.
Jul 30 08:30:01 ip-172-31-43-23 systemd[1]: Failed to start uWSGI Emperor service.
c3jack iT邦新手 5 級 ‧ 2020-07-30 17:12:31 檢舉

我有試著把restart 改成:

Restart=on-failure
StartLimitBurst=2
StartLimitInterval=30

但服務仍無法運作
P.S. 補上uwsgi & mkdir 成功執行的訊息:

Process: 917 ExecStart=/usr/local/bin/uwsgi --emperor /home/ubuntu/uwsgi-sites/ --daemonize /home/ubuntu/logs/uwsgi-emperor.log (code=exited, status=0/SUCCESS)
  Process: 913 ExecStartPre=/bin/bash -c mkdir -p /run/uwsgi; chown www-data:www-data /run/uwsgi (code=exited, status=0/SUCCESS)
c3jack iT邦新手 5 級 ‧ 2020-07-30 17:20:57 檢舉

執行journalctl -u uwsgi.service得到的訊息:

Jul 30 08:59:48 ip-172-31-43-23 systemd[1]: Starting uWSGI Emperor service...
Jul 30 08:59:48 ip-172-31-43-23 systemd[1]: uwsgi.service: Failed with result 'protocol'.
Jul 30 08:59:48 ip-172-31-43-23 systemd[1]: Failed to start uWSGI Emperor service.
Jul 30 08:59:48 ip-172-31-43-23 systemd[1]: uwsgi.service: Service hold-off time over, scheduling restart.
Jul 30 08:59:48 ip-172-31-43-23 systemd[1]: uwsgi.service: Scheduled restart job, restart counter is at 1.
Jul 30 08:59:48 ip-172-31-43-23 systemd[1]: Stopped uWSGI Emperor service.
Jul 30 08:59:48 ip-172-31-43-23 systemd[1]: Starting uWSGI Emperor service...
Jul 30 08:59:49 ip-172-31-43-23 systemd[1]: uwsgi.service: Failed with result 'protocol'.
Jul 30 08:59:49 ip-172-31-43-23 systemd[1]: Failed to start uWSGI Emperor service.
Jul 30 08:59:49 ip-172-31-43-23 systemd[1]: uwsgi.service: Service hold-off time over, scheduling restart.
Jul 30 08:59:49 ip-172-31-43-23 systemd[1]: uwsgi.service: Scheduled restart job, restart counter is at 2.
Jul 30 08:59:49 ip-172-31-43-23 systemd[1]: Stopped uWSGI Emperor service.
Jul 30 08:59:49 ip-172-31-43-23 systemd[1]: uwsgi.service: Start request repeated too quickly.
Jul 30 08:59:49 ip-172-31-43-23 systemd[1]: uwsgi.service: Failed with result 'protocol'.
Jul 30 08:59:49 ip-172-31-43-23 systemd[1]: Failed to start uWSGI Emperor service.
froce iT邦大師 1 級 ‧ 2020-08-01 10:48:02 檢舉

抱歉,因為忙一直沒回。

這應該是權限問題,你程式碼資料夾的權限先打開試試。
你可以參照這篇
https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-uwsgi-and-nginx-on-ubuntu-16-04
試試看。

c3jack iT邦新手 5 級 ‧ 2020-08-01 17:53:09 檢舉

果然是權限問題,感謝

我要發表回答

立即登入回答