scl启用python27 bash

ger*_*ion 23 bash cron software-collections

我遇到了一个shell脚本,它打算在Redhat 6服务器上每隔30分钟在cron上运行一次.shell脚本基本上只是一个运行python脚本的命令.

服务器上的本机版本python是2.6.6,但是这个特定脚本所需的python版本是python 2.7+.我可以使用"scl"命令在命令行上轻松运行它(此示例包含用于显示版本更改的python -V命令):

$ python -V
Python 2.6.6
$ scl enable python27 bash
$ python -V
Python 2.7.3
Run Code Online (Sandbox Code Playgroud)

此时我可以在命令行上运行python 2.7.3脚本没问题.

这是障碍.

当您发出scl enable python27 bash命令时,它会启动一个新的bash shell会话,该会话(再次)适用于交互式命令行工作.但是当在shell脚本中执行此操作时,只要它运行bash命令,脚本就会因新会话而退出.

这是失败的shell脚本:

#!/bin/bash
cd /var/www/python/scripts/
scl enable python27 bash
python runAllUpserts.py >/dev/null 2>&1
Run Code Online (Sandbox Code Playgroud)

它只是在它到达第4行时就会停止,因为"bash"将它从脚本中弹出并进入一个新的bash shell.所以它永远不会看到我需要它运行的实际python命令.

另外,如果每30分钟运行一次,这将每次添加一个新的bash,这是另一个问题.

由于几个原因,我不愿意立即将服务器上的本机python版本更新为2.7.3.Redhat yum repos还没有python 2.7.3,手动安装将在yum更新系统之外.根据我的理解,yum本身运行在python 2.6.x上.

这是我找到使用scl的方法的地方

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

小智 25

在SCL环境中使用一个heredoc做一切是最好的选择,IMO:

scl enable python27 - << \EOF
cd /var/www/python/scripts/
python runAllUpserts.py >/dev/null 2>&1
EOF
Run Code Online (Sandbox Code Playgroud)

另一种方法是直接在scl环境中运行第二个命令(这是唯一一个使用Python的命令):

cd /var/www/python/scripts/
scl enable python27 "python runAllUpserts.py >/dev/null 2>&1"
Run Code Online (Sandbox Code Playgroud)


wen*_*nhn 17

scl enable python27 bash 激活python虚拟环境.

您可以在bash脚本中执行此操作,只需获取SCL包的虚拟环境的启用脚本,该脚本位于 /opt/rh/python27/enable

例:

#!/bin/bash
cd /var/www/python/scripts/
source /opt/rh/python27/enable
python runAllUpserts.py >/dev/null 2>&1
Run Code Online (Sandbox Code Playgroud)


Joh*_*ohn 7

是不是最简单的直接你的python脚本?test_python.py:

#!/usr/bin/env python

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

然后在你的crontab中:

2 * * * *    scl python27 enable $HOME/test_python.py
Run Code Online (Sandbox Code Playgroud)

确保你可以test_python.py执行.

另一种方法是调用调用python的shell脚本.test_python.sh:

#/bin/bash
python test_python.py
Run Code Online (Sandbox Code Playgroud)

在您的crontab中:

2 * * * *   scl python27 enable $HOME/test_python.sh
Run Code Online (Sandbox Code Playgroud)


小智 5

一个班轮

scl enable python27 'python runAllUpserts.py >/dev/null 2>&1'
Run Code Online (Sandbox Code Playgroud)

我也将它与 CentOS 6.x 上的 devtoolsets 一起使用

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.
Run Code Online (Sandbox Code Playgroud)