Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is the script, /home/jack/myscript.sh, which I want to run:

export JAVA_HOME=/usr/lib/jvm/java-6-sun-
/home/jack/myapp/apache-tomcat-6.0.32/bin/./catalina.sh run

The upstart file, /etc/init/myapp.conf, that I have written:

  description "Myapp Server"
  start on runlevel [2345]
  stop on runlevel [06]
  respawn limit 10 5
  exec `su jack -c /home/jack/myscript.sh`

So, if I want to run the script as user jack, would my above configuration work? Is there a better way of doing it?

share|improve this question
up vote 5 down vote accepted

According to the Upstart cookbook, a built-in feature to run a service as a different user is planned but not implemented yet.

If you have start-stop-daemon, use it.

exec start-stop-daemon --start -u jack --exec /home/jack/myscript.sh

Otherwise, use su (as you did) or sudo. Note that start-stop-daemon always switches to the indicated user (barring errors), whereas su and sudo obey their own policy (in particular, the PAM policies on most systems).

Note that in your case, you don't need the intermediate script. You can set environment variables directly from the Upstart service definition.

description "Myapp Server"
  start on runlevel [2345]
  stop on runlevel [06]
  respawn limit 10 5
  env JAVA_HOME=/usr/lib/jvm/java-6-sun-
  exec /home/jack/myapp/apache-tomcat-6.0.32/bin/catalina.sh run
share|improve this answer
How do I set environment with start-stop-daemon and will I be able to write log to a file with it? – nixnotwin Nov 26 '11 at 6:17

As of Upstart 1.4, Upstart has the ability to run a System Job as a specified user using the setuid and setgid stanzas, e.g exec start-stop-daemon --start -c myuser --exec command,

Otherwise you might use tricks like exec sudo -u myuser command

More information here

share|improve this answer
Try to summarize the content of links in addition to posting them. This way your post remains useful even if the external link goes down. – jw013 Oct 17 '12 at 15:15

If you want to run it as session user just create a session job (as of 1.7) instead of a system job. Such jobs are not as restrictive as system jobs when it comes to environment variables and are executed as the session's user.

share|improve this answer

Your Answer


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.