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 Answers 2
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.
7 Comments
cd at the start of the script?cd in the Python script? Is that possible?import os and then print os.getcwd(). If the directory needs to change, then use os.chdir(fullpath) to set it.This was a bug in Calibre that was fixed in subsequent versions.
#!/usr/bin/env python, but to no avail.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