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.

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?

share|improve this question
add comment

2 Answers

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
  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
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
add comment

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
add comment

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.