1

OpenShift has these default dir's:

# $_ENV['OPENSHIFT_INTERNAL_IP']  - IP Address assigned to the application
# $_ENV['OPENSHIFT_GEAR_NAME']  - Application name
# $_ENV['OPENSHIFT_GEAR_DIR']   - Application dir
# $_ENV['OPENSHIFT_DATA_DIR']  - For persistent storage (between pushes)
# $_ENV['OPENSHIFT_TMP_DIR']   - Temp storage (unmodified files deleted after 10 days)

How do reference them in a python script? Example script "created a log file in log directory and log in data directory?

from time import strftime

now= strftime("%Y-%m-%d %H:%M:%S")
fn  = "${OPENSHIFT_LOG_DIR}/test.log"
fn2 = "${OPENSHIFT_DATA_DIR}/test.log"
#fn  = "test.txt"

input = "appended text " + now + " \n"

with open(fn, "ab")  as f: 
       f.write(input)
with open(fn2, "ab") as f: 
       f.write(input)

Can these script be used with cron?

EDIT the BASH File:

#! /bin/bash

#date >> ${OPENSHIFT_LOG_DIR}/new.log
source $OPENSHIFT_HOMEDIR/python-2.6/virtenv/bin/activate
python file.py
date >> ${OPENSHIFT_DATA_DIR}/new2data.log

2 Answers 2

3
import os

os.getenv("OPENSHIFT_INTERNAL_IP") 

should work.

So with your example, modify to:-

import os
OPENSHIFT_LOG_DIR = os.getenv("OPENSHIFT_LOG_DIR") 
fn  = os.path.join(OPENSHIFT_LOG_DIR, "test.log")

And, yes, you can call this python script with a cron by referencing your bash script if you want... Like this for example:-

 #!/bin/bash
 date >> ${OPENSHIFT_LOG_DIR}/status.log
 chmod +x status
 cd ${OPENSHIFT_REPO_DIR}/wsgi/crawler
 nohup python file.py 2>&1 &
Sign up to request clarification or add additional context in comments.

8 Comments

Can this file.py be in the cron directories or does this file.py have to be referenced in file.sh script?
You can directly call this script in your cron by referring to it as python /path/to/your/file.py in your crontab. Cron will use the python program to execute this script as expected.
OpenShift doesnt have crontab. I have file.py in cron/minutely.
Executing your python script and relinquishing control after that so there's no stdout and the program runs in the background. See cyberciti.biz/tips/…
Are you able to execute python file.py in your openshift server after ssh-ing in your bash environment?
|
2

Those variables OPENSHIFT_* are provided as environment variables on OpenShift -- so the $_ENV["OPENSHIFT_LOG_DIR"] is an example to get the value inside a php script.

In python, the equivalent would just be os.getenv("OPENSHIFT_LOG_DIR"). Made edits to Calvin's post above and submitted 'em.

Re: the question of where file.py exists -- use os.getenv("OPENSHIFT_REPO_DIR") as the base directory where all your code would be located on the gear where you app is running. So if your file is located in .openshift/misc/file.py -- then just use:

os.path.join(os.getenv("OPENSHIFT_REPO_DIR"), ".openshift", "misc", "file.py")  

to get the full path.

Or in bash, the equivalent would be:

$OPENSHIFT_REPO_DIR/.openshift/misc/file.py   

HTH

1 Comment

Once you clarified that your env variables in your question above are php variables, the syntax changes below. Updated my answer.

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.