0

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?

simonw16
  • 960
  • 9
  • 25
  • 1
    Possible duplicate of [Why does sudo change the PATH?](https://stackoverflow.com/q/257616/608639) – jww May 30 '19 at 04:11
  • Maybe, it doesn't deserve a downvote that quick. I had that vote less than 5 seconds after it was posted. Assuming you didn't read the content. – simonw16 May 30 '19 at 04:12
  • Checked, I am not using sudo. This is not a duplicate. – simonw16 May 30 '19 at 04:16

1 Answers1

1

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.

Kurtis Rader
  • 6,734
  • 13
  • 20