无法通过 sh / crontab 启动 python 程序
Not able to start python program via sh / crontab
我尝试从脚本或作为 cronjob 启动一个名为 ocrmypdf 的 python 程序。
它在终端上完美运行,
pi@piscan:~ $ ocrmypdf
usage: ocrmypdf [-h] [--verbose [VERBOSE]] [--version] [-L FILE] [-j N] [-n]
[--flowchart FILE] [-l LANGUAGE] [--title TITLE]
[--author AUTHOR] [--subject SUBJECT] [--keywords KEYWORDS]
[-d] [-c] [-i] [--oversample DPI] [-f] [-s]
[--skip-big MPixels] [--tesseract-config TESSERACT_CONFIG]
[--pdf-renderer {auto,tesseract,hocr}]
[--tesseract-timeout TESSERACT_TIMEOUT] [-k] [-g]
input_file output_file
ocrmypdf: error: the following arguments are required: input_file, output_file
但从另一个 shell 它因我不明白的原因而中断。
pi@piscan:~ $ sh ocrmypdf
sh: 0: Can't open ocrmypdf
pi@piscan:~ $ which ocrmypdf
/usr/local/bin/ocrmypdf
pi@piscan:~ $ sh $(which ocrmypdf)
import: unable to open X server `' @ error/import.c/ImportImageCommand/364.
import: unable to open X server `' @ error/import.c/ImportImageCommand/364.
from: can't read /var/mail/ocrmypdf.main
/usr/local/bin/ocrmypdf: 10: /usr/local/bin/ocrmypdf: Syntax error: "(" unexpected (expecting "then")
这是执行的代码:
pi@piscan:~ $ cat $(which ocrmypdf)
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import re
import sys
from ocrmypdf.main import run_pipeline
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(run_pipeline())
当您键入 sh ocrmypdf
时,您会询问 sh
shell(可能是 /bin/sh
,这通常是 /bin/bash
或 /bin/dash
的符号链接) 来解释 ocrmypdf
文件,它是 Python 脚本,而不是 shell 脚本。
因此 运行 python ocrmypdf
或 python $(which ocrmypdf)
或者使 ocrmypdf
脚本可执行。然后(至少 Linux)execve(2) will start the python interpreter, because of the shebang.
当然,ocrmypdf
脚本应该在您的 PATH
并且 crontab
作业不在您的桌面环境中 运行。所以他们无权访问您的 X11 server Xorg (or to Wayland,如果您正在使用它)。您可以为此显式设置 DISPLAY
变量,但我不建议这样做。
我尝试从脚本或作为 cronjob 启动一个名为 ocrmypdf 的 python 程序。
它在终端上完美运行,
pi@piscan:~ $ ocrmypdf
usage: ocrmypdf [-h] [--verbose [VERBOSE]] [--version] [-L FILE] [-j N] [-n]
[--flowchart FILE] [-l LANGUAGE] [--title TITLE]
[--author AUTHOR] [--subject SUBJECT] [--keywords KEYWORDS]
[-d] [-c] [-i] [--oversample DPI] [-f] [-s]
[--skip-big MPixels] [--tesseract-config TESSERACT_CONFIG]
[--pdf-renderer {auto,tesseract,hocr}]
[--tesseract-timeout TESSERACT_TIMEOUT] [-k] [-g]
input_file output_file
ocrmypdf: error: the following arguments are required: input_file, output_file
但从另一个 shell 它因我不明白的原因而中断。
pi@piscan:~ $ sh ocrmypdf
sh: 0: Can't open ocrmypdf
pi@piscan:~ $ which ocrmypdf
/usr/local/bin/ocrmypdf
pi@piscan:~ $ sh $(which ocrmypdf)
import: unable to open X server `' @ error/import.c/ImportImageCommand/364.
import: unable to open X server `' @ error/import.c/ImportImageCommand/364.
from: can't read /var/mail/ocrmypdf.main
/usr/local/bin/ocrmypdf: 10: /usr/local/bin/ocrmypdf: Syntax error: "(" unexpected (expecting "then")
这是执行的代码:
pi@piscan:~ $ cat $(which ocrmypdf)
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import re
import sys
from ocrmypdf.main import run_pipeline
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(run_pipeline())
当您键入 sh ocrmypdf
时,您会询问 sh
shell(可能是 /bin/sh
,这通常是 /bin/bash
或 /bin/dash
的符号链接) 来解释 ocrmypdf
文件,它是 Python 脚本,而不是 shell 脚本。
因此 运行 python ocrmypdf
或 python $(which ocrmypdf)
或者使 ocrmypdf
脚本可执行。然后(至少 Linux)execve(2) will start the python interpreter, because of the shebang.
当然,ocrmypdf
脚本应该在您的 PATH
并且 crontab
作业不在您的桌面环境中 运行。所以他们无权访问您的 X11 server Xorg (or to Wayland,如果您正在使用它)。您可以为此显式设置 DISPLAY
变量,但我不建议这样做。