从init.d脚本运行时,Virtualenv库在python脚本中的链接错误,但在正常运行时不是

时间:2018-10-02 03:57:10

标签: python virtualenv cherrypy amazon-linux sysv

这是低谷。

  • 首先,我在Amazon Linux(原始实例,而不是Amazon Linux 2)上使用EC2实例,将我限制为sysV而不是systemd。
  • 我有一个运行在CherryPy WSGI服务器上的Flask应用程序服务器,前面是Nginx反向代理。
  • WSGI服务器和应用程序服务器都在python3 virtualenv中。
  • 服务器在virtualenv中运行正常。
  • 输入#!服务器文件顶部的virtualenv python bin位置的一行允许我通过输入服务器脚本的完整路径从系统上的任何位置运行服务器(无需首先激活virtualenv):/path/to/server.py < / li>
  • 但是,当我尝试从init.d脚本中运行此脚本时,出现了ModuleNotFoundError: No module named 'cherrypy'错误。
  • 我还尝试使用activate_this.py达到相同的结果。

以下是一些(匿名)代码,以查看是否有帮助:

server.py:

#!/path/to/myVenv/bin/python

# Activate virtualenv from within this script:
# Commented out as it doesn't seem to help
# activate_this = "/path/to/myVenv/bin/activate_this.py"
# exec(compile(open(activate_this, "rb").read(), activate_this, 'exec'), dict(__file__=activate_this))

# Import your application as:
# from wsgi import application
# Example:

from wsgi import application

# Import CherryPy
import cherrypy

if __name__ == '__main__':

    # Mount the application
    cherrypy.tree.graft(application, "/")

    # Other CherryPy stuff here. Not relevant as script doesn't progress this far
    # when it breaks and works fine when it doesn't

/etc/init.d/crowded(基于https://www.cyberciti.biz/tips/linux-write-sys-v-init-script-to-start-stop-service.html

#!/bin/bash
#
# chkconfig: 35 90 12
# description: CrowdEd server
#

# Based on file found at
# https://www.cyberciti.biz/tips/linux-write-sys-v-init-script-to-start-stop-service.html

PATH=/path/to/myVenv/bin

# Get function from functions library
. /etc/init.d/functions

# Start the service FOO
start() {
        initlog -c "echo -n Starting CrowdEd server: "
        /path/to/server.py &
        ### Create the lock file ###
        touch /var/lock/subsys/crowded
        success $"CrowdEd server startup"
        echo
}

# Restart the service FOO
stop() {
        initlog -c "echo -n Stopping CrowdEd server: "
        killproc server.py
        ### Now, delete the lock file ###
        rm -f /var/lock/subsys/crowded
        echo
}

### main logic ###
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status crowded
        ;;
  restart|reload|condrestart)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac

exit 0

TL; DR:从init.d脚本调用时,从控制台编写的Python脚本不起作用,特别是从init脚本调用时,无法链接virtualenv中的库,尽管从控制台调用时也可以很好地链接它们。我不知道为什么。

0 个答案:

没有答案