我有一个芹菜任务,它做:
subprocess.check_call([script.sh, 'clean'], cwd=module_folder, stdout=fd_log_out, stderr=fd_log_err)
当我从命令行运行工作程序时,一切都正常。
当芹菜工人从主管开始工作时,我在日志中得到了这样的信息:
[2016-05-09 10:31:30,129: ERROR/MainProcess] Task project_server.executions.tasks.execution_run[04e6295b-2aa4-4384-9218-fccb111a20df] raised unexpected: TypeError('expect bytes or str, not NoneType',)
Traceback (most recent call last):
File "/home/benjamin/.virtualenvs/project/lib/python3.4/site-packages/celery/app/trace.py", line 240, in trace_task
R = retval = fun(*args, **kwargs)
File "/home/benjamin/.virtualenvs/project/lib/python3.4/site-packages/celery/app/trace.py", line 438, in __protected_call__
return self.run(*args, **kwargs)
File "/home/benjamin/Project/projectserver/project_server/executions/tasks.py", line 88, in execution_run
fd_log_err=fd_log_err)
File "/home/benjamin/Project/projectcommon/project_common/generator/__init__.py", line 807, in create_application
fd_log_out, fd_log_err)
File "/home/benjamin/Project/projectcommon/project_common/models/extensions.py", line 550, in build_scala_module
subprocess.check_call([script.sh, 'clean'], cwd=module_folder, stdout=fd_log_out, stderr=fd_log_err)
File "/usr/lib/python3.4/subprocess.py", line 556, in check_call
retcode = call(*popenargs, **kwargs)
File "/usr/lib/python3.4/subprocess.py", line 537, in call
with Popen(*popenargs, **kwargs) as p:
File "/usr/lib/python3.4/subprocess.py", line 859, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.4/subprocess.py", line 1381, in _execute_child
executable = os.fsencode(executable)
File "/home/benjamin/.virtualenvs/project/lib/python3.4/os.py", line 770, in fsencode
raise TypeError("expect bytes or str, not %s" % type(filename).__name__)
TypeError: expect bytes or str, not NoneType
为什么会失败?
好的,我得到了:script.sh不在主管给出的$PATH中
要修复通过systemd运行python脚本时出现的相同错误,请将WorkingDirectory包含到服务文件中:
[Unit]
Description=Python script
After=multi-user.target
[Service]
WorkingDirectory=/path/to/script
User=user
Group=group
Type=idle
ExecStart=/path/to/python runner.py
[Install]
WantedBy=multi-user.target
如果你这样写(没有WorkingDirectory的脚本的完整路径)
ExecStart=/path/to/python /path/to/script/runner.py
你会有错误的。