參考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
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應該就能開機啟動了,不能的話檢查路徑、權限看看。
froce大師,感謝您的回答,但還是開不起來,請問要去那裹看error log?
我剛確定有建立 /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?
我打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.
我有試著把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)
執行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.
抱歉,因為忙一直沒回。
這應該是權限問題,你程式碼資料夾的權限先打開試試。
你可以參照這篇
https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-uwsgi-and-nginx-on-ubuntu-16-04
試試看。
果然是權限問題,感謝