My Python script hosted in OpenShift inside the .openshift / cron / minutely directory won't run. What's wrong?
I wrote the following script that sends an email to a specific email address and stores it in the .openshift / cron / minutely directory:
import smtplib
g = smtplib.SMTP('smtp.gmail.com:587')
g.ehlo()
g.starttls()
g.ehlo()
g.login('myusername','mypassword')
g.sendmail('myemail','otheremail','message')
Then I pushed the script to the server.
I expected the program to run once a minute and receive email every minute. However, there is no evidence that my code is running. Any idea what might be causing the problem? Did I forget a step when setting up my application?
Note. I have verified that the email and password I provided were correct and that cron was set.
EDIT: The problem seems to originate from the server: I removed the original file content by creating "testfile.txt" and wrote this code instead:
a = open('testfile.txt','r+')
if not a.read():
a.write('Test writing')
a.close()
after waiting for the code to run and ssh-ing to the server, I went to the named directory app-root/logs
and displayed content cron.log
that looked something like this:
Sat Nov 8 11:01:11 EST 2014: START minutely cron run
__________________________________________________________________________
/var/lib/openshift/545a6ac550044652510001d3/app-root/runtime/repo//.openshift/cron/minutely/test_openshift.py:
/var/lib/openshift/545a6ac550044652510001d3/app-root/runtime/repo//.openshift/cron/minutely/test_openshift.py: line 1: syntax error near unexpected token `('
/var/lib/openshift/545a6ac550044652510001d3/app-root/runtime/repo//.openshift/cron/minutely/test_openshift.py: line 1: `a = open('testfile.txt','r+')'
__________________________________________________________________________
Sat Nov 8 11:01:11 EST 2014: END minutely cron run - status=0
__________________________________________________________________________
Maybe the server is not interpreting the code in my file as Python code? Any suggestions are greatly appreciated.
source to share
connect to openshift console
rhc ssh app_name
Change to a directory that has script creation permission:
cd $OPENSHIFT_DATA_DIR
create test01.py script
touch test01.py
Grant execute permission to test01.py
chmod +x test01.py
Change script
nano test01.py
Add some simple code like
print("Hello")
run script:
./test01.py
Mistake:
./test01.py: line 1: syntax error near unexpected token `"Hello"'
./test01.py: line 1: `print("Hello")'
Now check the python path
which python
Output
/var/lib/openshift/your-sesseion-id/python/virtenv/venv/bin/python
Now add it to test01.py
#!/var/lib/openshift/your-sesseion-id/python/virtenv/venv/bin/python
print("Hello")
Now execute it
./test01.py
Output:
Hello
Conclusion: Your script needs to know how to run and where the python path is, so add it to the first line of your script
source to share