Python 项目主管';s目录选项不工作

Python 项目主管';s目录选项不工作,python,supervisord,Python,Supervisord,我想由主管执行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.lo

我想由主管执行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
textfile = open('./textfile')
[program:test]
directory=/root/test
command=python /root/test/test.py
在python脚本中,我使用了这样的相对路径

[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
textfile = open('./textfile')
[program:test]
directory=/root/test
command=python /root/test/test.py
我可以通过
/root/test
目录上的
python./test.py
成功执行此python脚本。 但当我开始管理时,我犯了这个错误

python: can't open file './test.py': [Errno 2] No such file or directory
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'
接下来,我在supervisor.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
textfile = open('./textfile')
[program:test]
directory=/root/test
command=python /root/test/test.py
我犯了这个错误

python: can't open file './test.py': [Errno 2] No such file or directory
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'
回溯(最近一次呼叫最后一次):
文件“/root/test/test.py”,第6行,在
textfile=打开(“./textfile”)
IOError:[Errno 2]没有这样的文件或目录:'./textfile'

无法设置脚本执行的目录吗?

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


请参见

youchange
test.py
将pwd更改为脚本的pwd这似乎是解决我的问题的最简单的方法,至少现在是这样。谢谢