2

I am seeing some strange behavior that has led me to wonder how the environment differs when running either of these 2 commands, versus using su username and then running the commands as the user one by one.

su username -c "cd /opt/scripts/ ; ./script.sh"

sudo -u username bash -c "cd /opt/scripts/ ; ./script.sh"

I am running these commands as root from an init script on a CentOS 6 Linux server.

I should specify further that the problem is that the script.sh file, which runs a java program, uses a classpath directive with relative paths (relative to where script.sh) lives (i.e ./classes/com:./lib/common ). If I su to username, then cd to the /opt/scripts and run ./script.sh it works, but the su and sudo commands within the script seem to both have an issue where the classes are not found, even if I cd to that directory before running the script. Why would that be? How is it different from using su to become the user and running the commands one by one?

3

Try comparing the environments:

$ sudo -s
# diff <(su username -c env) <(sudo -u username bash -c env)

For example, the environment for sudo is controlled by env_* defaults in /etc/sudoers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.