【问题标题】:Supervisor - program's directory option not working主管 - 程序的目录选项不起作用
【发布时间】:2015-12-07 07:25:36
【问题描述】:

我想由主管执行 python 脚本。

我在 supervisord.conf 中设置了目录选项 我在这样的命令选项中使用了相对路径。

[supervisord]
http_port=/var/tmp/supervisor.sock ; (default is to run a UNIX domain socket server)
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB       ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10          ; (num of main logfile rotation backups;default 10)
loglevel=info               ; (logging level;default info; others: debug,warn)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false              ; (start in foreground if true;default false)
minfds=1024                 ; (min. avail startup file descriptors;default 1024)
minprocs=200                ; (min. avail process descriptors;default 200)
directory=/root             ; (default is not to cd during start)

[supervisorctl]
serverurl=unix:///var/tmp/supervisor.sock ; use a unix:// URL  for a unix socket

[program:test]
directory=/root/test
command=python ./test.py
autostart=true

在 python 脚本中,我使用了这样的相对路径。

textfile = open('./textfile')

我可以通过python ./test.py/root/test 目录上成功执行这个python 脚本。 但是当我启动主管时,我得到了这个错误。

python: can't open file './test.py': [Errno 2] No such file or directory

接下来,我像这样在 supervisor.conf 的命令选项中使用了绝对路径。

[program:test]
directory=/root/test
command=python /root/test/test.py

我得到了这个错误。

Traceback (most recent call last):
  File "/root/test/test.py", line 6, in <module>
    textfile = open('./textfile')
IOError: [Errno 2] No such file or directory: './textfile'

有没有办法设置脚本执行的目录?

【问题讨论】:

标签: python supervisord


【解决方案1】:

除了在test.py 中使用textfile 的绝对路径外,您可以通过在调用textfile = open('./textfile') 之前将以下内容添加到test.py 来将当前工作目录更改为脚本的目录:

import os

abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)

python: Change the scripts working directory to the script's own directory

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 2014-07-13
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多