scl 启用 python27 bash

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16631461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:43:54  来源:igfitidea点击:

scl enable python27 bash

bashcronsoftware-collections

提问by gerion

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.

我在 Redhat 6 服务器上的 cron 中每 30 分钟运行一次 shell 脚本时遇到了障碍。shell 脚本基本上只是一个运行 python 脚本的命令。

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 是 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

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

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

Here's the snag.

这是障碍。

When you issue the scl enable python27 bashcommand 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.

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

Here's the shell script that is failing:

这是失败的shell脚本:

#!/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.

它会在到达第 4 行时立即停止,因为“bash”将它从脚本中弹出并放入一个新的 bash shell。所以它永远不会看到我需要它运行的实际 python 命令。

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

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

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.

由于几个原因,我现在不愿意将服务器上的原生 python 版本更新到 2.7.3。Redhat yum 存储库还没有 python 2.7.3,手动安装将在 yum 更新系统之外。据我了解,yum 本身在 python 2.6.x 上运行。

Here's where I found the method for using scl

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

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

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

回答by slavek

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

在 SCL 环境中的一个 heredoc 中做所有事情是最好的选择,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:

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

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

回答by wenjianhn

scl enable python27 bashactivates a python virtual environment.

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

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

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

Example:

例子:

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

回答by John

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

直接只是你的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()

then in your crontab:

然后在您的 crontab 中:

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

Make sure you make test_python.pyexecutable.

确保你可以test_python.py执行。

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

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

#/bin/bash
python test_python.py

in your crontab:

在您的 crontab 中:

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

回答by Dr.Bokko

One liner

一个班轮

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

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

我也将它与 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.

回答by keithpjolley

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

scl是最愚蠢的“让我们试着把你锁在‘我已经看到了一段时间的废话中。

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 
# cd /usr/bin
# ln -s skeleton pepper
# pepper foo bar
)" 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/pepperyou can do this:

所以如果你想运行一个脚本,比如说,/opt/rh/python27/root/usr/bin/pepper你可以这样做:

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

and it should work as expected.

它应该按预期工作。

回答by Jim Dennis

I've only seen this sclstuff 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.

我以前只见过scl一次这些东西,并且无法随时访问安装了它的系统。但我认为它只是以某种方式设置 PATH 和其他一些环境变量,与它们在virtualenv.

Perhaps changing the script to have the bashsubprocess call pythonwould work:

也许更改脚本以进行bash子进程调用python会起作用:

##代码##

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

在subprocess 的 shell上python找到的实例bash应该是你的 2.7.x 副本......并且所有其他环境设置完成的scl都应该由此继承。