从mac中的php运行bash脚本时,$ USER为空

I want to exec() a bash script.sh from index.php.

When I run the script from terminal:

  • $USER is admin.
  • security unlock-keychain works.
  • codesign without --timestamp=none works.

When I run the script from php:

  • $USER is empty
  • security unlock-keychain does not work.
  • codesign needs --timestamp=none to work.

I have changed httpd.conf:

  • User admin
  • Group Admin

USER is not a magic variable that returns your username. It's just set for your convenience by login, the program that authenticates interactive terminal logins.

From macOS man 1 login:

The login utility enters information into the environment (see environ(7)) 
specifying the user's home directory (HOME), command interpreter (SHELL),
search path (PATH), terminal type (TERM) and user name (both LOGNAME and USER).

If you don't run in this context, these variables may not be available.

You should either update the bash script to get the username in a more robust way (id -un), or you can do login's work in PHP with putenv("USER=" . get_current_user());