2

I have a python script with #!/usr/bin/python in the first line. I can run it from CLI with python myScp.py.

But as part of cron script. The python script fails to run. The cron is tested, runs python script and can write to /tmp/crontest.txt

It seems that there is a directory problem. I tested with os.getcwd(). Its correct...Just when cron runs the script it throws an error. Running from CLI: /usr/bin/python myScp.py throws the same error.

Traceback (most recent call last):
 File "/myScp.py", line 31, in <module>
 execfile(dn2 + 'anotherScpt.py')
IOError: [Errno 2] No such file or directory: './anotherScpt.py'
elethan
17.1k10 gold badges70 silver badges97 bronze badges
asked Jan 1, 2013 at 15:56
6
  • Is your version of Python actually at that path? Try #!/usr/bin/env python instead - this should be better at getting the right path regardless of system setup. Also, try executing the file with ./myScp.py as that is what cron will essentially be doing. Commented Jan 1, 2013 at 15:57
  • 1
    It's pretty clear the error is with the other file not being found. Is that file there? Are you ensuring it is looking in the right path? Commented Jan 1, 2013 at 16:01
  • Yes. I running the script with python myscp.py everything runs fine. Commented Jan 1, 2013 at 16:02
  • How do you fill dn2? ./ refers to current directory, and I'm quite positive that you're assuming it to be something else than cron actually uses. Commented Jan 1, 2013 at 16:03
  • @favoretti doesnt python use the local directory by deflaut?? Commented Jan 1, 2013 at 16:11

2 Answers 2

10

Our preferred way is to specify explicitly the working dir as well in the crontab entries:

0 0 * * * cd /my/project; /opt/python-2.7/bin/python bin/myscript.py
answered Jan 1, 2013 at 16:02
Sign up to request clarification or add additional context in comments.

Comments

2

Given the error, your problem is that you are relying on the program being in a particular directory to execute another file.

When you run the program in the directory it is in, it can find the file - when you (or cron) runs it outside that directory, it can't find that file. You need to put the file somewhere the script can find it, use an absolute path, or find the location of the script in the program.

answered Jan 1, 2013 at 16:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.