3
votes

I wrote the following script, which sends an email to a specific email address, and saved it inside 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')

I then pushed the script to the server.

I expected the program to run once every minute, and receive an email every minute. However, there is no evidence indicating that my code is being run. Any idea what might be causing the problem? Did I forget a step while setting up my application?

Note: I've checked that the email address and password I provided were correct, and that cron is installed.

EDIT: It seems that the problem is originating from the server: I deleted the original contents of the file, created '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 into the server, I changed to the directory named app-root/logs and displayed the contents of cron.log, which 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
__________________________________________________________________________

Could it be that the server is not interpreting the code in my file as python code? Any suggestions welcome.

1
Your approach is wrong. Try to produce a minimal reproduction of your problem. You could for example try to write something in a file from your script to narrow your problem down. The problem could come from your library, some security at GMail or anything. - AsTeR
Yes you should try to logging some information which can help you to debug. - Tanveer Alam
Thank you both. I'll try that and get back to you - loic17
I changed a few things. Would you care to take a look at the edits I made to the original post? Thank you for your time. - loic17
I have never tried running raw python code in cron file before. You might need a directive at the first line of the file to tell it you are running python. Not sure. In the past I run my python scripts from cron like so: "python myscript.py" - fat fantasma

1 Answers

3
votes

connect to openshift console

rhc ssh app_name

Change to a directory to have permission to create script:

cd $OPENSHIFT_DATA_DIR

create test01.py script

touch test01.py

Give executing permission to test01.py

chmod +x test01.py

Edit script

nano test01.py

Add a simple code like

print("Hello")

run script:

./test01.py

Error:

./test01.py: line 1: syntax error near unexpected token `"Hello"'
./test01.py: line 1: `print("Hello")'

Now check python path

which python

Output

/var/lib/openshift/your-sesseion-id/python/virtenv/venv/bin/python

Now add a she bang 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 should know how to run and where is python path, so add it at the first line of your script