scl enable python27 bash

29

我在Redhat 6服务器上的cron中设置了一个每30分钟运行一次的shell脚本,但遇到了问题。该shell脚本基本上只是用于运行Python脚本的命令。

服务器上原生版本的Python是2.6.6,但这个特定脚本需要使用Python 2.7+。我可以轻松地通过使用"scl"命令在命令行上运行它(下面的示例包括Python -V命令以显示版本更改):

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

目前我可以在命令行上运行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

当它到达第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/

6个回答

29

在SCL环境中,将所有内容都放入一个heredoc中是最好的选择,这是我的意见:

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

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

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

23

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

1
这在我的经验中效果很好 - 只需要一行代码更改,enable脚本非常简单。要查看任何集合的路径,请使用以下命令:scl enable python27 'which python' - RichVel

8

直接使用您的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()

然后在你的crontab中:

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

请确保将test_python.py设置为可执行文件。

另一种选择是调用一个调用python的shell脚本。test_python.sh

#/bin/bash
python test_python.py

在你的crontab中:

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

6

一句话简介

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

我也在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.

1

scl 是我最近看到的最愚蠢的“试图让你陷入困境”的东西。

以下是我如何使得我可以传递参数给一系列链接到单个骨架文件的脚本:

$ 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}"

如果你想运行一个脚本,它位于 /opt/rh/python27/root/usr/bin/pepper,那么你可以这样做:

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

并且它应该按预期工作。


0

我之前只看过一次这个 scl 的东西,而且没有安装它的系统。但我认为它只是以某种方式设置了 PATH 和其他环境变量,类似于在 virtualenv 下完成的方式。

也许将脚本更改为让 bash 子进程调用 python 会起作用:

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

子进程 bash 的 shell 中找到的 python 实例应该是你的 2.7.x 版本 ... 而由 scl 执行的所有其他环境设置都应该被继承。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接