Q:

Upstart script runs well with sudo but fails as root user

I have a simple nodeJs upstart service run as,
script
  exec /usr/bin/node /path/to/server.js
end script

On running with sudo command, it works
~$ sudo service upserv start
sg_gui_server start/running, process 19321

When it is ran as a root user, it fails with bizarre error. 
~$ sudo su
sk# service sg_gui_server restart
karaf: JAVA_HOME not set; results may vary

JAVA_HOME not set, this error is bizarre as nodeJs is not dependent on java libs and this error comes, when i run a simple python server as well.
How does running a service with sudo prefix differ from running it as a root user?

A:

This depends on your configuration, but it appears that your local environment variable for JAVA_HOME is being used from your user. You can verify this by checking for the env_keep settings in /etc/sudoers. 
I would personally source /etc/profile as part of your upstart script. 

