dongzhan5246 2019-05-30 04:09
浏览 198

Linux $ PATH对于同一用户是不同的,具体取决于它的访问方式

I have a php script running in a cronjob on the server.

However, I am unexpectedly getting different $PATH from the same user, depending on how I execute the command.

I log in as user ubuntu:

ubuntu@:$ echo $PATH 
/home/ubuntu/bin:/home/ubuntu/.local/bin:/home/ubuntu/.nvm/versions/node/v12.3.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

I then sudo su bitbucket:

bitbucket@:$ echo $PATH
/home/bitbucket/.nvm/versions/node/v12.3.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

I execute a script from a cronjob running as bitbucket and output the following debug to a log file:

$ whoami
bitbucket

The above proves the user is bitbucket, then:

$ echo $PATH
/usr/bin:/bin

Please note I am not running as sudo. I am utilising sudo to switch user, but not using sudo to echo $PATH.

How is it that the same user has 2 different $PATH variables?

  • 写回答

1条回答 默认 最新

  • donglilian0061 2019-05-30 23:48
    关注

    You didn't say which shell you're using so I'm going to assume it's bash. The first, and perhaps most important, thing to note is that when you run sudo su bitbucket you're getting an interactive shell. Which means that ~/.bashrc will be sourced. Lots of people modify PATH in that script. Something that tends to cause problems. Why? Because non-interactive shells, such as the one launched by cron to run your command, won't read ~/.bashrc.

    Your cron job gets a PATH equivalent to running this command: sudo su bitbucket -c 'echo $PATH'. Play around with that to get a better understanding of how this works. For example, instead of echo $PATH try env.

    评论

报告相同问题?

悬赏问题

  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表