python don't extract zipfile when ran through crontab

Jayesh Patil
#!/usr/bin/python
import requests, zipfile, StringIO, sys
extractDir = "myfolder"
zip_file_url = "download url"
response = requests.get(zip_file_url)
zipDocument = zipfile.ZipFile(StringIO.StringIO(response.content))
zipinfos = zipDocument.infolist()
for zipinfo in zipinfos:
    extrat = zipDocument.extract(zipinfo,path=extractDir)

System configuration

  1. Ubuntu OS 16.04
  2. Python 2.7.12

$ python extract.py when I run the code on Terminal with above command, it works properly and create the folder and extract the file into it.

Similarly, when I create a cron job using sodu rights the code executes but don't create any folder or extracts the files.

crontab command:-

40 10 * * * /usr/bin/sudo /usr/bin/python /home/ubuntu/demo/directory.py > /home/ubuntu/demo/logmyshit.log 2>&1

also tried

40 10 * * * /usr/bin/python /home/ubuntu/demo/directory.py > /home/ubuntu/demo/logmyshit.log 2>&1

Notes :

  1. I check the syslog, it says the cron is running successfully
  2. The above code gives no errors
  3. also made the python program executable by chmod +x filename.py

Please help where am I going wrong.

Serge Ballesta

Oups, there is nothing really wrong in running a Python script in crontab, but many bad things can happen because the environment is not the one you are used to.

When you type in an interactive shell python directory.py, the PATH and all required PYTHON environment variable have been set as part of login and interactive shell initialization, and the current directory is your home directory by default or anywhere you currently are.

When the same command is run from crontab, the current directory is not specified (but may not be what you expect), PATH is only /bin:/usr/bin and python environment variables are not set. That means that you will have to tweak environment variables in crontab file until you get a correct Python environment, and set the current directory.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python zipfile.extract() doesn't extract all files

From Dev

Where are python logs default stored when ran through IPython notebook?

From Dev

Processes don't terminate when I call Python script using crontab

From Dev

Ruby Crontab Rake task fails, but only when command is ran by crontab

From Dev

Python subprocess doesn't work when ran by launchd

From Dev

arp doesn't run in script when run through crontab

From Dev

Possible reasons why a Python script may be ran manually but not with crontab?

From Dev

When earphones are plugged in don't play notification sound through the speakers

From Dev

Don't run second function if first function is ran

From Dev

wkhtmltopdf segfault when ran from python

From Dev

Shell doesn't run python ONLY when executed by crontab

From Java

ZipFile inside a ZipFile (Python)

From Dev

Python zipfile dosen't release zip file

From Java

Python zipfile extract files from directory inside a zip file

From Dev

Two different results for same JS,when ran through Browser's JS Console and through a JS File

From Dev

Python String Format not working as expected when ran in a script

From Dev

value set for modal don't display when it is set through ajax success function

From Dev

Apex Set: How to iterate through a set when you don't know it's data type?

From Dev

Ran .NET 4.61 installer, still don't see new framework profile in new project dialog?

From Dev

Bash scripts ran from from gnome/nautilus don't have environment variables

From Dev

Ran a for loop to create several symlinks at once, two of them failed and I don't see why

From Dev

Python code don’t work when launched by cron

From Dev

Run python in terminal and don't terminate when terminal is closed

From Dev

Python subprocess don't output to file when called in a loop

From Dev

Zenity does not show messages in deb preinst when it is ran through Ubuntu Software Center

From Dev

PHP Zip Archive Extract Not working When Script is Executed by Crontab

From Dev

Python script in bash with crontab doesn't work but without crontab it does

From Dev

Why quantificators don't pass through xargs

From Dev

Exceptions don't bubble through instances in PHP?

Related Related

  1. 1

    Python zipfile.extract() doesn't extract all files

  2. 2

    Where are python logs default stored when ran through IPython notebook?

  3. 3

    Processes don't terminate when I call Python script using crontab

  4. 4

    Ruby Crontab Rake task fails, but only when command is ran by crontab

  5. 5

    Python subprocess doesn't work when ran by launchd

  6. 6

    arp doesn't run in script when run through crontab

  7. 7

    Possible reasons why a Python script may be ran manually but not with crontab?

  8. 8

    When earphones are plugged in don't play notification sound through the speakers

  9. 9

    Don't run second function if first function is ran

  10. 10

    wkhtmltopdf segfault when ran from python

  11. 11

    Shell doesn't run python ONLY when executed by crontab

  12. 12

    ZipFile inside a ZipFile (Python)

  13. 13

    Python zipfile dosen't release zip file

  14. 14

    Python zipfile extract files from directory inside a zip file

  15. 15

    Two different results for same JS,when ran through Browser's JS Console and through a JS File

  16. 16

    Python String Format not working as expected when ran in a script

  17. 17

    value set for modal don't display when it is set through ajax success function

  18. 18

    Apex Set: How to iterate through a set when you don't know it's data type?

  19. 19

    Ran .NET 4.61 installer, still don't see new framework profile in new project dialog?

  20. 20

    Bash scripts ran from from gnome/nautilus don't have environment variables

  21. 21

    Ran a for loop to create several symlinks at once, two of them failed and I don't see why

  22. 22

    Python code don’t work when launched by cron

  23. 23

    Run python in terminal and don't terminate when terminal is closed

  24. 24

    Python subprocess don't output to file when called in a loop

  25. 25

    Zenity does not show messages in deb preinst when it is ran through Ubuntu Software Center

  26. 26

    PHP Zip Archive Extract Not working When Script is Executed by Crontab

  27. 27

    Python script in bash with crontab doesn't work but without crontab it does

  28. 28

    Why quantificators don't pass through xargs

  29. 29

    Exceptions don't bubble through instances in PHP?

HotTag

Archive