loic17
loic17

Reputation: 787

My Python script hosted on OpenShift inside the .openshift/cron/minutely directory doesn't run. What's wrong?

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.

Upvotes: 3

Views: 1311

Answers (1)

Serjik
Serjik

Reputation: 10971

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

Upvotes: 3

Related Questions