Shell Script to Start Tomcat on Reboot Using Non-Root User
January 30th, 2009 by Andrew Chen
Since Siusic.com is now sitting on its own dedicated Linux server I have to learn a lot of Linux shell commands to manage the Linux server myself. One of the thing that I wanted
When the shell script is put into the /ect/int.d directory it will be called by the system at boot time and passed “start” as parameter. That is how Linux start a program automatically at boot time. It is analogous to add programs to the Windows registry \HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run in order to start a program automatically when Windows boot up.
I wrote my first ever Linux shell script “tomcat” to do that. I post the script here because I didn’t find a straight forward answer when I Google it or Baidu it. Hopefully it will help some new Tomcat and Linux users.
ARGV=”$@”
STARTTOMCAT=’Tomcat_Installation_Path/bin/startup.sh’
STOPTOMCAT=’Tomcat_Installation_Path/bin/shutdown.sh’
source /What_Ever_Path/tomcat.env
case $ARGV in
start)
echo $”Starting Tomcat”
su tomcat $STARTTOMCAT
ERROR=$?
;;
stop)
echo $”Stopping Tomcat2″
su tomcat $STOPTOMCAT
ERROR=$?
;;
esac
exit $ERROR
That is it. The script first store the start-up and shut-down script for Tomcat in variable
It then execute the command in tomcat.env to set environment variables. The tomcat.env file is like this
export JAVA_HOME=Java_Installation_Path
export PATH=$JAVA_HOME/bin:$PATH
export CATALINA_HOME=Tomcat_Installation_Path
export CATALINA_BASE=Tomcat_Instance_Path
After that the script execute the start-up or shut down script using user Tomcat. So prior to that you will have to create a user called tomcat which has proper permission to the tomcat installation directory and the tomcat instance directory.
Put the tomcat script file into /etc/init.d directory. Make it executable using the chmod command. Don’t forgot to create a link to the tomcat file in the /etc/rc5.d directory. After that Tomcat will start automatically at on boot time as user tomcat.


i cant copy or paste or even save in the init.d file directory.