2

For testing purposes, I have a very simple python script which creates a text file, named with current datetime so I can tell when it has been ran:

#!/usr/bin/python

from time import gmtime, strftime

try:

    filename = strftime("%Y-%m-%d %H:%M:%S", gmtime())
    f = open(filename+'.txt', 'w')
    f.write('HelloWorld')
    f.close()
except StopIteration:
    print "An error has occurred.."

I have placed the file here and it has full permissions:

usr/share/pyshared/scripts/test_script.py

When i run the script manually with the following, the script runs and creates my test text file:

python ~/../../usr/share/pyshared/scripts/test_script.py

In my crontab file I have the following which does not run (should run every minute for testing purposes).

* * * * * python ~/../../usr/share/pyshared/scripts/test_script.py

First time experimenting with crontab and fairly new to linux as well so forgive me if I've missed something fairly obvious here, Thanks in advance.

1 Answer 1

5

Use absolute paths in crontab:

* * * * * /usr/bin/python /usr/share/pyshared/scripts/test_script.py

Also, specify absolute path in your script:

filename = "/home/MYUSER/"+strftime("%Y-%m-%d %H:%M:%S", gmtime())
f = open(filename+'.txt', 'w')
Sign up to request clarification or add additional context in comments.

1 Comment

some explanation: cron uses a different shell and executes with an empty environment. without absolute paths, it won't even find the Python interpreter since there is no PATH variable defined.

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.