我尝试从脚本或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())
答案 0 :(得分:2)
当您输入sh ocrmypdf
时,您要求sh
shell(可能/bin/sh
通常是/bin/bash
或/bin/dash
的符号链接)来解释ocrmypdf
1}}文件是 Python 脚本,而不是shell脚本。
因此,运行python ocrmypdf
或python $(which ocrmypdf)
或使ocrmypdf
脚本可执行。然后(至少在Linux上)execve(2)将start the python interpreter,因为shebang。
当然,ocrmypdf
脚本应位于PATH
crontab
作业未在桌面环境中运行。因此,如果您使用的话,他们无法访问您的X11服务器Xorg(或Wayland。您可以为此明确设置DISPLAY
变量,但我不建议这样做。