我尝试从脚本或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
但是由于另一个我不了解的原因,它破裂了。
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
外壳程序(可能是/bin/sh
,通常是/bin/bash
或/bin/dash
的符号链接)解释ocrmypdf
文件,该文件是Python脚本,而不是外壳程序。
因此,运行python ocrmypdf
或python $(which ocrmypdf)
或使ocrmypdf
脚本可执行。然后,由于execve(2),(至少在Linux上)start the python interpreter将shebang。
当然,ocrmypdf
脚本应该在您的PATH
中
并且crontab
作业不在您的桌面环境中运行。因此,他们无权访问您的X11服务器Xorg(如果正在使用,则无法访问Wayland)。您可以为此显式设置DISPLAY
变量,但是我不建议您这样做。