【问题标题】:Environment variable value not getting using getenv() in PHP when PHP file runs from a bash script当 PHP 文件从 bash 脚本运行时,环境变量值未在 PHP 中使用 getenv()
【发布时间】:2020-01-15 05:34:31
【问题描述】:

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: 

我已经尝试了所有的 stackoverflow 解决方案,但没有结果。

/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


【问题讨论】:

    标签: php bash ubuntu environment-variables


    【解决方案1】:

    /etc/profile 仅当 bash 是交互式或非交互式时才被读取,但使用 --login 选项调用。我会启动守护进程

    bash --login ./StartPHPDaemon
    

    并且,为了调试,做一个

    echo CONFIGPATH=$CONFIGPATH
    

    在那个脚本中。

    【讨论】:

      猜你喜欢
      • 2016-05-19
      • 2018-07-11
      • 2013-08-08
      • 1970-01-01
      • 2019-12-12
      • 1970-01-01
      • 2015-07-05
      • 1970-01-01
      • 2017-10-26
      相关资源
      最近更新 更多