0

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())
bbk
  • 45
  • 4
  • 1
    Cron jobs do not have access to the X11 server. This is a common FAQ. – tripleee Jan 02 '16 at 10:58
  • This was actually not the problem. The program doesn't need X11, and I don't know why this error message appears. – bbk Jan 02 '16 at 12:09
  • Ah, the `import` command wants X11, but you don't really want to run this shell command at all. – tripleee Jan 02 '16 at 12:57
  • Ah, okay, that makes sense. Thanks for explaining, I was really confused by this one. – bbk Jan 02 '16 at 13:21

1 Answers1

2

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.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • I wasn't aware that a call with an interpreter ignores the shebang. python3 /path/to/script.py works fine! – bbk Jan 02 '16 at 12:10