赞
踩
最近在Linux服务器上面部署我写的应用程序,使用的是虚拟环境。
具体Linux服务代码如下:
- [Unit]
- Description=Process Redis data on the SMS_Receive website.
- After=network.target
- [Service]
- User=ubuntu
- Group=www-data
- Environment="PATH=/var/SMS_Receive/venv/bin/"
- WorkingDirectory=/var/SMS_Receive_Service
- ExecStart=python Anti_Web_Scraping.py
- ExecReload=/bin/kill -s HUP $MAINPID
- ExecStop=/bin/kill -s TERM $MAINPID
- [Install]
- WantedBy=multi-user.target
运行服务之后报如下错误:
[/etc/systemd/system/SMS_Receive_Redis.service:9] Executable path is not absolute, ignoring: python Anti_Web_Scraping
具体截图如下:
之后参考了这个页面:
Your service definitions are like this:
- [Service]
- ExecStart=python /home/pi/projects/script1.py
- Restart=always
And this is the error message on each of them:
Executable path is not absolute, ignoring: python /home/pi/...
For systemd
, the "executable" in this service definition is python
, and that is clearly not an absolute path. The /home/pi/projects/script1.py
is just an argument to this python
executable, and caring about its proper form is the executable's job.
Any Python-specific environment variables like PYTHONPATH
have no meaning at all for systemd
: you must give it an absolute path for the executable in the service definition, each and every time.
Typically, the absolute path to the python interpreter is /usr/bin/python
, but you can check:
- $ type python
- python is /usr/bin/python
So your service definitions should be like this:
- [Service]
- ExecStart=/usr/bin/python /home/pi/projects/script1.py
- Restart=always
页面地址在这里:Using systemd to prevent python scripts from crashing
主要是相对路径的问题,需要修改代码如下:
- [Unit]
- Description=Process Redis data on the SMS_Receive website.
- After=network.target
- [Service]
- User=ubuntu
- Group=www-data
- Environment="PATH=/var/SMS_Receive/venv/bin/"
- WorkingDirectory=/var/SMS_Receive_Service
- ExecStart=/usr/bin/env python Anti_Web_Scraping.py
- ExecReload=/bin/kill -s HUP $MAINPID
- ExecStop=/bin/kill -s TERM $MAINPID
- [Install]
- WantedBy=multi-user.target
上面只是加了一个/usr/bin/env
制定了使用用户虚拟环境来解释python脚本。当然也可以加 -p
参数:
还可以加上-P参数来指定一些目录去寻找python这个程序, #!/usr/bin/env -S -P/usr/local/bin:/usr/bin -p的作用就是在/usr/local/bin和/usr/bin目录下寻找python。
/usr/bin/env -P ${Environmnet} python Anti_Web_Scraping.py
作者自己遇到的问题与该博主类似,只是我是执行 java -jar命令时遇到的错误。原理是一样的:
原命令:ExecStart=java -agentlib:linux -jar /opt/gsp/gspserver-encrypt.jar
修改后:ExecStart=/usr/bin/java -agentlib:linux -jar /opt/gsp/gspserver-encrypt.jar
欢迎大家私信博主,拉你进技术交流群
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。