[英]How do you call python command “workon” from PHP exec?
我正在从CLI运行PHP脚本。 在那个PHP脚本中,我想使用system()
从命令行执行一些命令。 也许这不是适合这项工作的工具,我可能只是将PHP脚本包装在shell脚本中,但我想知道是否可以在PHP脚本中完全完成。
当我在终端中执行此操作时,它可以正常工作:
user@host:~$ workon pootle
(pootle)user@host:~$
但是这不适用于PHP(作为同一个用户执行)。
#!/usr/bin/env php
<?php
system('workon pootle'); // sh: 1: workon: not found
system('/bin/bash workon pootle'); // /bin/bash: workon: No such file or directory
system('/bin/sh workon pootle'); // /bin/sh: 0: Can't open workon
我注意到最后两个的输出与终端执行时的输出完全相同,但第一个输出是不同的。 我认为它可能是别名,但它似乎不是。 alias | grep -i workon
alias | grep -i workon
显示没有输出。
我还比较了从命令行env
和PHP system('env')
返回的所有环境变量,除_
之外的所有变量都完全相同,包括SHELL=/bin/bash
。
终端执行的which workon
输出为空。
编辑
也许我已经到了这个地方。
user@host:~$ type workon
workon is a function
workon ()
{
typeset env_name="$1";
if [ "$env_name" = "" ]; then
lsvirtualenv -b;
return 1;
fi;
virtualenvwrapper_verify_workon_home || return 1;
virtualenvwrapper_verify_workon_environment $env_name || return 1;
activate="$WORKON_HOME/$env_name/$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate";
if [ ! -f "$activate" ]; then
echo "ERROR: Environment '$WORKON_HOME/$env_name' does not contain an activate script." 1>&2;
return 1;
fi;
type deactivate > /dev/null 2>&1;
if [ $? -eq 0 ]; then
deactivate;
unset -f deactivate > /dev/null 2>&1;
fi;
virtualenvwrapper_run_hook "pre_activate" "$env_name";
source "$activate";
virtualenvwrapper_original_deactivate=`typeset -f deactivate | sed 's/deactivate/virtualenv_deactivate/g'`;
eval "$virtualenvwrapper_original_deactivate";
unset -f deactivate > /dev/null 2>&1;
eval 'deactivate () {
# Call the local hook before the global so we can undo
# any settings made by the local postactivate first.
virtualenvwrapper_run_hook "pre_deactivate"
env_postdeactivate_hook="$VIRTUAL_ENV/$VIRTUALENVWRAPPER_ENV_BIN_DIR/postdeactivate"
old_env=$(basename "$VIRTUAL_ENV")
# Call the original function.
virtualenv_deactivate $1
virtualenvwrapper_run_hook "post_deactivate" "$old_env"
if [ ! "$1" = "nondestructive" ]
then
# Remove this function
unset -f virtualenv_deactivate >/dev/null 2>&1
unset -f deactivate >/dev/null 2>&1
fi
}';
virtualenvwrapper_run_hook "post_activate";
return 0
}
bash /usr/share/virtualenvwrapper/virtualenvwrapper.sh workon
我通过安装virtualenvwrapper
找到了这个,然后我跑了
env | grep VIRTU
VIRTUALENVWRAPPER_PROJECT_FILENAME=.project
VIRTUALENVWRAPPER_SCRIPT=/usr/share/virtualenvwrapper/virtualenvwrapper.sh
VIRTUALENVWRAPPER_HOOK_DIR=/root/.virtualenvs
_VIRTUALENVWRAPPER_API= mkvirtualenv rmvirtualenv lsvirtualenv showvirtualenv workon add2virtualenv cdsitepackages cdvirtualenv lssitepackages toggleglobalsitepackages cpvirtualenv setvirtualenvproject mkproject cdproject mktmpenv
并且看到了VIRTUALENVWRAPPER_SCRIPT
......很容易从那里弄清楚
为了能够执行virtualenvwrapper功能,我们首先需要source
的virtualenvwrapper.sh
文件。 要查找该文件的位置,请在终端执行:
which virtualenvwrapper.sh
如果这不产生任何输出,请使用find
来查找它。
find /usr/ -name "virtualenvwrapper.sh"
对我来说,位置是/usr/local/bin/virtualenvwrapper.sh
当PHP进行系统调用时,它使用sh
:
<?php
system('echo $0'); // echoes 'sh'
但是,使用sh
执行virtualenvwrapper.sh
脚本失败,所以我们需要使用bash
。
另外需要注意的是PHP系统调用是彼此独立的子shell,所以如果你这样做:
<?php
system('FOO=BAR');
system('echo $FOO');
这打印出一个空行。 以下打印BAR
:
<?php
system('FOO=BAR && echo $FOO');
因此,我们需要:
workon
以更改工作虚拟环境 这就是我提出的并且似乎正常工作:
<?php
$commands = [
'source /usr/local/bin/virtualenvwrapper.sh',
'workon pootle',
'pootle update_stores --project=AIWeb',
];
system('/bin/bash -c "'.implode(' && ', $commands) .'"');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.