29

I've hit a snag with a shell script intended to run every 30 minutes in cron on a Redhat 6 server. The shell script is basically just a command to run a python script.

The native version python on the server is 2.6.6 but the python version required by this particular script is python 2.7+. I am able to easily run this on the command line by using the "scl" command (this example includes the python -V command to show the version change):

$ python -V
Python 2.6.6
$ scl enable python27 bash
$ python -V
Python 2.7.3

At this point I can run the python 2.7.3 scripts on the command line no problem.

Here's the snag.

When you issue the scl enable python27 bash command it starts a new bash shell session which (again) is fine for interactive commandline work. But when doing this inside a shell script, as soon as it runs the bash command, the script exits because of the new session.

Here's the shell script that is failing:

#!/bin/bash
cd /var/www/python/scripts/
scl enable python27 bash
python runAllUpserts.py >/dev/null 2>&1

It simply stops as soon as it hits line 4 because "bash" pops it out of the script and into a fresh bash shell. So it never sees the actual python command I need it to run.

Plus, if run every 30 minutes, this would add a new bash each time which is yet another problem.

I am reluctant to update the native python version on the server to 2.7.3 right now due to several reasons. The Redhat yum repos don't yet have python 2.7.3 and a manual install would be outside of the yum update system. From what I understand, yum itself runs on python 2.6.x.

Here's where I found the method for using scl

http://developerblog.redhat.com/2013/02/14/setting-up-django-and-python-2-7-on-red-hat-enterprise-6-the-easy-way/

6 Answers 6

29

Doing everything in one heredoc in the SCL environment is the best option, IMO:

scl enable python27 - << \EOF
cd /var/www/python/scripts/
python runAllUpserts.py >/dev/null 2>&1
EOF

Another way is to run just the second command (which is the only one that uses Python) in scl environment directly:

cd /var/www/python/scripts/
scl enable python27 "python runAllUpserts.py >/dev/null 2>&1"
23

scl enable python27 bash activates a python virtual environment.

You can do this from within a bash script by simply sourcing the enable script of the virtual environment, of the SCL package, which is located at /opt/rh/python27/enable

Example:

#!/bin/bash
cd /var/www/python/scripts/
source /opt/rh/python27/enable
python runAllUpserts.py >/dev/null 2>&1
1
  • 1
    This works well in my experience - only a single line change and the enable script is very simple. To see the path for any collection, use a command like this: scl enable python27 'which python'
    – RichVel
    Nov 13, 2019 at 17:44
8

Isn't the easiest to just your python script directly? test_python.py:

#!/usr/bin/env python

import sys
f = open('/tmp/pytest.log','w+')
f.write(sys.version)
f.write('\n')
f.close()

then in your crontab:

2 * * * *    scl python27 enable $HOME/test_python.py

Make sure you make test_python.py executable.

Another alternative is to call a shell script that calls the python. test_python.sh:

#/bin/bash
python test_python.py

in your crontab:

2 * * * *   scl python27 enable $HOME/test_python.sh
6

One liner

scl enable python27 'python runAllUpserts.py >/dev/null 2>&1'

I use it also with the devtoolsets on the CentOS 6.x

me@my_host:~/tmp# scl enable devtoolset-1.1 'gcc --version'
gcc (GCC) 4.7.2 20121015 (Red Hat 4.7.2-5)
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1

scl is the dumbest "let us try and lock you in` nonsense I've seen in a while.

Here's how I made it so I could pass arguments to a series of scripts that all linked to a single skeleton file:

$ cat /usr/bin/skeleton
#!/bin/sh

tmp="$( mktemp )"
me="$( basename $0 )"
echo 'scl enable python27 - << \EOF' >> "${tmp}"
echo "python '/opt/rh/python27/root/usr/bin/${me}' $@" >> "${tmp}"
echo "EOF" >> "${tmp}"
sh "${tmp}"
rm "${tmp}"

So if there's a script you want to run that lives in, say, /opt/rh/python27/root/usr/bin/pepper you can do this:

# cd /usr/bin
# ln -s skeleton pepper
# pepper foo bar

and it should work as expected.

0

I've only seen this scl stuff once before and don't have ready access to a system with it installed. But I think it's just setting up PATH and some other environment variables in some way that vaguely similar to how they're done under virtualenv.

Perhaps changing the script to have the bash subprocess call python would work:

#!/bin/bash
cd /var/www/python/scripts/
(scl enable python27 bash -c "python runAllUpserts.py") >/dev/null 2>&1

The instance of python found on the subprocess bash's shell should be your 2.7.x copy ... and all the other environmental settings done by scl should be inherited thereby.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.