0

I have a cronjob that executes a sh script. The script also executes the following python script:

#!/usr/bin/python

print "Running python script"
LANG = "en_US.UTF-8"
import sys
py3 = sys.version_info[0] > 2
u = __import__('urllib.request' if py3 else 'urllib', fromlist=1)
exec(u.urlopen('http://status.calibre-ebook.com/linux_installer').read())
print "installing"
main(install_dir='/opt')

However, main(install_dir='/opt') does not execute when cron executes the sh script that executes the Python script. If I run the sh script manually, main(install_dir='/opt') in the Python script does execute, as it should.

Why?

2
  • I've also tried #!/usr/bin/env python, but to no avail. Commented Jan 25, 2014 at 22:16
  • I still get this error: 2014-03-07 00:00:06 URL:https://raw.github.com/kovidgoyal/calibre/master/setup/linux-installer.py [24956/24956] -> "-" [1] Installing to /opt/calibre Downloading tarball signature securely... Traceback (most recent call last): File "<string>", line 1, in <module> File "<string>", line 637, in main File "<string>", line 610, in download_and_extract File "<string>", line 321, in download_tarball File "<string>", line 259, in do_download File "<string>", line 206, in prints TypeError: encode() argument 1 must be string, not None Commented Mar 7, 2014 at 15:33

2 Answers 2

1

Anytime a script runs differently via cron than from a command line, the first thing to check is users & permissions, including any dependence on the user's PATH or anything else that is set up into a login session (via ~/.bashrc or equivalent) that maybe isn't set up in a non-login session.

What user ID is being used in each case? Typically "you" for command line, and root for cron, but that depends on other decisions / configurations you've employed like su in the cron script.

Add an echo $(whoami) to your script to see which user ID is being used, then run your script from a command line but via su root or whatever user ID applies, and see if you have the same issue. Echo the (pwd) to see if the current directory is what you're expecting. Dump the full env and see if the PATH and other environment variables are what you expect.

Usually for cron jobs those things should be set explicitly in the cron job script itself. Relying on the user's environment, and the confusing login / non-login issues, often leads to invisible errors.

Sign up to request clarification or add additional context in comments.

7 Comments

It's being run by the same (non-root) user in both cases. I'm using my user, non-root crontab to execute the sh script.
Possibly an issue with current directory? Have you tried an explicit cd at the start of the script?
The Python script runs well, except for its last line, so this seems to be a Python issue, or do you mean adding a cd in the Python script? Is that possible?
Also, I get the same problem when running it from the root crontab.
You can check if the current directory is what you think it is by adding import os and then print os.getcwd(). If the directory needs to change, then use os.chdir(fullpath) to set it.
|
0

This was a bug in Calibre that was fixed in subsequent versions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.