Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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