5

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

export JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.26/
/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
  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?

3 Answers 3

5

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
  respawn limit 10 5
  env JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.26/
  exec /home/jack/myapp/apache-tomcat-6.0.32/bin/catalina.sh run
2
  • 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, 2011 at 6:17
  • This is an old answer. The setuid and setgid are already available Feb 4, 2017 at 7:05
4

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:

setuid username
setgid groupname

On previous versions we can use User Jobs, which are stored in the user home folder.

Or use some application in the exec command.

If start-stop-daemon is available:

exec start-stop-daemon --start -c myuser --exec command

With sudo:

exec sudo -u myuser command

With su:

exec su -s /bin/sh -c 'exec "$0" "$@"' myuser -- command [parameters...]

More information here

1
  • 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, 2012 at 15:15
0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .