简体   繁体   中英

Not able to start python program via sh / crontab

I try to start a python program called ocrmypdf from a script or as a cronjob.

It works perfectly from the terminal,

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

but from another shell it breaks for reasons I do not understand.

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")

This is the executed code:

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())

When you type sh ocrmypdf you ask the sh shell (probably /bin/sh which is often a symlink to /bin/bash or /bin/dash ) to interpret the ocrmypdf file which is a Python script, not a shell one.

So either run python ocrmypdf or python $(which ocrmypdf) or make the ocrmypdf script executable. Then (on Linux at least) execve(2) will start the python interpreter , because of the shebang .

Of course, the ocrmypdf script should be in your PATH

And crontab jobs are not running in your desktop environment. So they don't have access to your X11 server Xorg (or to Wayland , if you are using it). You could explicitly set the DISPLAY variable for that, but I don't recommend doing this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM