How to start Tomcat as non root on boot

12,215

Solution 1

Use JSVC, read Tomcat JSVC manual or simply install package tomcat7.

Solution 2

Create a config file tomcat.conf and put it under /etc/init

/etc/init contains configuration files used by Upstart

the tomcat.conf will look like the following lines (Note: Replace JAVA_HOME and CATALINA_HOME with your own ):

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 apache-tomcat
  setgid www-data

  # adapt paths - Replace with your Paths:
  env JAVA_HOME=/usr/lib/jvm/jdk1.7.0_13/
  env CATALINA_HOME=/home/mohammed/apache-tomcat-7.0.27/

  # adapt java options to suit your needs:
  env JAVA_OPTS="-Djava.awt.headless=true -Xms512M -Xmx1024M -server -XX:+UseParallelGC"

  exec $CATALINA_HOME/bin/catalina.sh run

  # cleanup temp directory after stop
  post-stop script 
    rm -rf $CATALINA_HOME/temp/*
  end script
Share:
12,215
perak
Author by

perak

Updated on June 04, 2022

Comments

  • perak
    perak almost 2 years

    How to start Tomcat under different user on boot? I've tried the following command but it prompts for password.

    su -c "/etc/tomcat/bin/catalina.sh start" tomcat
    

    I'm using Tomcat 7.0.40 and Ubuntu 12.04.