[英]Start a python script at startup automatically?
我遵循了關於stackoverflow的多個教程,這些教程關於在啟動時啟動python腳本,但是它們都不起作用。
我需要激活一個virtualenv然后啟動Flask服務器
我在/etc/init.d/
了一個start.sh
#!/bin/sh
### BEGIN INIT INFO
# Provides: skeleton
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Should-Start: $portmap
# Should-Stop: $portmap
# X-Start-Before: nis
# X-Stop-After: nis
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Interactive: true
# Short-Description: Example initscript
# Description: This file should be used to construct scripts to be
# placed in /etc/init.d.
### END INIT INFO
cd /home/ion/
source /home/ion/py35/bin/activate
cd /home/ion/Desktop/flask/
nohup python main.py &
echo "Done"
它的權限是chmod在+ x
ion@aurora:/etc/init.d$ ll start.sh
-rwxr-xr-x 1 root root 625 Jun 25 19:10 start.sh*
去了/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.
/etc/init.d/start.sh
exit 0
沒工作
cronjob方法
須藤crontab -e
並附加
@reboot sh '/etc/init.d/start.sh'
也沒有工作,我在哪里錯?
手動觸發的日志
(py35) ion@aurora:~/Desktop/flask$ python main.py
WARNING:tensorflow:From /home/ion/Desktop/flask/encoder.py:57: calling l2_normalize (from tensorflow.python.ops.nn_impl) with dim is deprecated and will be removed in a future version.
Instructions for updating:
dim is deprecated, use axis instead
2018-06-25 19:34:05.511943: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
* Serving Flask app "main" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://localhost:5505/ (Press CTRL+C to quit)
* Restarting with stat
1)不要使用舊的“ init.d”方法。 使用現代的東西。 如果您具有Ubuntu 15.04和更高版本,則可以使用Systemd創建守護程序,該守護程序將在啟動時自動啟動。 例如,如果您的Ubuntu早於15.04,請使用Upstart。
對於Systemd:
在/lib/systemd/system/you_service_name.service
創建具有以下內容的單元文件(據我所知,您的python腳本在運行時不會產生新進程,因此Type
應該很simple
。 在此處了解更多信息):
[Unit]
Description=<your_service_name>
After=network.target network-online.target
[Service]
Type=simple
User=<required_user_name>
Group=<required_group_name>
Restart=always
ExecStartPre=/bin/mkdir -p /var/run/<your_service_name>
PIDFile=/var/run/<your_service_name>/service.pid
ExecStart=/path/to/python_executable /path/to/your/script.py
[Install]
WantedBy=multi-user.target
保存此文件並重新加載systemd:
sudo systemctl daemon-reload
然后將您的服務添加到自動啟動:
sudo systemctl enable you_service_name.service
您應該看到在enable
操作之后Systemd創建了所需的符號鏈接。
重新啟動並查看它是否已啟動並正在運行( ps aux | grep python
或sudo systemctl status you_service_name.service
)。 如果有什么奇怪的地方-檢查Systemd日志:
sudo journalctl -xe
UPD:
要在所需的virtualenv中啟動python腳本,只需在服務單元文件中使用以下表達式:
ExecStart=/venv_home/path/to/python /venv_home/path/to/your/script.py
2)您也可以使用crontab
,但是需要在此處為所需的shell指定完整路徑,例如:
@reboot /bin/bash /path/to/script.sh
如果您需要其他幫助-請在這里告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.