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?