当 PHP 文件从 bash 脚本运行时,环境变量值在 PHP 中没有使用 getenv()



StartPHPDaemon

#!/bin/bash
currdate=$(date +'%m%d%y')
curpath=$(readlink -f ${0%/*})
php_file=$curpath/ExecuteJobFromQueue.php
base_php=$(basename $php_file)
for pids in $(ps aux | grep $base_php  | grep -v grep | awk '{print $2 '})
do
kill $pids
echo "Killing $pids"
done

#echo $currdate          >> $curpath/logs/StartService_${currdate}.LOG
#date +'%R:%S'           >> $curpath/logs/StartService_${currdate}.LOG
nohup php $php_file "E" > /dev/null 2>&1 &

ExecuteJobFromQueue.php

...// rest of the code
$server_config_path=getenv('CONFIGPATH');
wh_log("Server config path: ".$server_config_path,"INFO");
...// rest of the code

我想从/etc/profile获取CONFIGPATH的值

当我从放置文件的路径运行StartPHPDaemon时,它使用以下命令获取值./StartPHPDaemon.

输出(日志(:

[15-Jan-2020 11:01:33]  INFO:   Server config path: /home/Project/Workspace/ConfigFile/dmimasterserver.config

但是当我使用命令从根路径运行它时sudo sh /home/Project/StartPHPDaemon值变为空白。

输出(日志(:

[15-Jan-2020 11:01:33]  INFO:   Server config path: 

我已经尝试了所有堆栈溢出解决方案,但没有结果。

/etc/profile

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
if [ "$PS1" ]; then
if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
# The file bash.bashrc already sets the default PS1.
# PS1='h:w$ '
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ "`id -u`" -eq 0 ]; then
PS1='# '
else
PS1='$ '
fi
fi
fi
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
JAVA_HOME=/usr/lib/jvm/jdk1.7.0
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH
CONFIGPATH="/home/Project/Workspace/ConfigFile/dmimasterserver.config"
export CONFIGPATH

/etc/profile 仅在 bash 是交互式或非交互式但使用--login选项调用时才读取。我会通过以下方式启动守护进程

bash --login ./StartPHPDaemon

并且,对于调试,执行

echo CONFIGPATH=$CONFIGPATH

在该脚本中。

最新更新