我正在从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
显示没有输出。
我还比较了从命令行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
}
答案 0 :(得分: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
......很容易从那里弄清楚
答案 1 :(得分:0)
为了能够执行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'
但virtualenvwrapper.sh
脚本在使用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) .'"');