3

I am trying to create a service for tomcat on Ubuntu 12.10 using upstart.

So, i created a tomcat.conf file in /etc/init

description "Tomcat Server"

start on runlevel [2345]
stop on runlevel [!2345]
respawn
respawn limit 10 5

# run as non privileged user 
# add user with this command:
## adduser --system --ingroup www-data --home /opt/apache-tomcat apache-tomcat
# Ubuntu 12.04: (use 'exec sudo -u apache-tomcat' when using 10.04)
setuid tomcat
setgid tomcat

pre-start script
    . /etc/default/tomcat
end script

exec $CATALINA_HOME/bin/catalina.sh run

# cleanup temp directory after stop
post-stop script
    rm -rf $CATALINA_HOME/temp/*
end script

The /etc/default/tomcat file contains env variables, which i need to set before starting the service.

However, sourcing the file has no effect. When I source the /etc/default/tomcat file form command line, it works fine. But when I include it in the script, it has no effect.

What could be wrong?

ufdeveloper
  • 387
  • 6
  • 19

3 Answers3

1

You'll probably need to do export each of the environment variables piecemeal. Something like this:

script
    . /etc/default/tomcat
    export CATALINA_HOME
    exec $CATALINA_HOME/bin/catalina.sh run
end script

should result in CATALINA_HOME being available to the catalina.sh file.

Femi
  • 64,273
  • 8
  • 118
  • 148
1

Nested scripts in upstart (e.g. pre-start) inherit the environment set in the enclosing file, but any changes made within the nested script will have no effect on the enclosing script's envrionment.

Source: http://upstart.ubuntu.com/cookbook/#environment-variables (see the long example near the end of that section)

Adam Fabicki
  • 530
  • 3
  • 10
1

before sourcing the file, set -a as explained here:

https://askubuntu.com/questions/711946/how-to-load-environment-variable-from-a-file-for-an-upstart-job

Community
  • 1
  • 1
JDiMatteo
  • 12,022
  • 5
  • 54
  • 65