1

I'm running a Python feedparser script via cron job on a Centos6 remote server (SSHing into the server).

In Crontab, this is my cron job:

MAILTO = [email protected]
*/10 * * * * /home/local/COMPANY/malvin/SilverChalice_CampusInsiders/SilverChalice_CampusInsiders.py > /home/local/COMPANY/malvin/SilverChalice_CampusInsiders`date +\%Y-\%m-\%d-\%H:\%M:\%S`-cron.log | mailx -s "Feedparser Output" [email protected]

However, I'm seeing this message in the email that's being sent, which should just contain the output of the script:

Null message body; hope that's ok
/usr/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
Traceback (most recent call last):
  File "/home/local/COMPANY/malvin/SilverChalice_CampusInsiders/SilverChalice_CampusInsiders.py", line 70, in <module>
    BC_01.createAndIngest(name, vUrl, tags, desc)
  File "/home/local/COMPANY/malvin/SilverChalice_CampusInsiders/BC_01.py", line 69, in createAndIngest
    creds = loadSecret()
  File "/home/local/COMPANY/malvin/SilverChalice_CampusInsiders/BC_01.py", line 17, in loadSecret
    credsFile=open('brightcove_oauth.json')
IOError: [Errno 2] No such file or directory: 'brightcove_oauth.json'

Normally, this would be a no-brainer issue: something must be wrong with my code. Except, the script works perfectly fine when I run it on the command line via python SilverChalice_CampusInsiders.py

What am I doing wrong here? Why doesn't the Python script "see" the json oauth file when run via cron job?

1 Answer 1

6

Cron sets a minimal environment for the jobs (and I think it runs the job from the home directory).

Inside your python script, when you do something like -

open('<filename>')

It checks for the filename in the current working directory , not the directory in which your scripts exist.

That is true even when running from commandline , if you change directory to some other directory (maybe your home directory) , and then use absolute path to your script to run it, you should be getting the same error.

Instead of depending on the current working directory to be correct and have the file you want to open, you can try either of the below options -

  1. Use an absolute paths to the files you want to open , do not use relative path.

  2. Or If the above is not an option for you, and the files you want to open are present relative to the script that is getting run (for example purpose lets say in the same directory) , then you can use __file__ (this gives the script location) and os.path , to create the absolute path to your file at runtime, Example -

    import os.path
    
    fdir = os.path.abspath(os.path.dirname(__file__)) #This would give the absolute path to the directory in which your script exists.
    f = os.path.join(fdir,'<yourfile')
    

At the end f would have the path to your file and you can use that to open your file.

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

10 Comments

Thanks for the detailed response, much appreciated. So if all three of my files are in the same directory (2 py scripts and a json oauth), would these two lines (which I'm assuming I should place in my main py script) look like this? fdir = os.path.abspath(/home/local/MYCOMPANY/malvin/SilverChalice_ CampusInsiders)
I just need to import the oauth.json file and some functions from another py file, do I now need to restructure my main py script so that they are opening these two other files with f = os.path.join(fdir,'<yourfile') ?
You can use __file__ , if you are not using that, then it would be easier to just use the complete (absolute path) when you are openning the file. This is for openning the file only, importing should be working fine (importing other .py files) .
If the other file I need my main py script to access is oauth.json should I use the f = os.path.join(fdir,'oauth.json') ? Right now in my main script I have credsFile=open('brightcove_oauth.json') and I keep getting the error in my email that's saying IOError: [Errno 2] No such file or directory: 'oauth.json' — should i just replace the credsFile line with the f = os.path..... line?
If the fdir is as you showed above (but with strings/quotes) , then yes
|

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.